-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathselectRandomFiles.R
More file actions
51 lines (41 loc) · 1.21 KB
/
Copy pathselectRandomFiles.R
File metadata and controls
51 lines (41 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
######### Select Random Files ####################
######### by Andrew Piper ####################
######### CC By 4.0 License ##################
#this script takes as input a directory of text files
#it outputs a random sample of files from that directory into a new directory
#you can add cleaning steps if you want to alter the files
#set working director where your source texts are located
source.texts<-c("")
target.texts<-c("")
setwd(source.texts)
#get list of files
fn<-list.files()
#subset by external list
#sub<-read.csv("")
#fn<-fn[fn %in% sub$FILENAME]
#sample n files
n<-40
fn.s<-sample(fn, n)
#loop through ingest and write
for (i in 1:length(fn.s)){
print(i)
#setwd to source texts directory
setwd(source.texts)
#ingest
work<-scan(fn.s[i], what="character", quote="", quiet=T)
### insert cleaning steps here ###
#remove numbers
#work<-gsub("\\d", "", work)
#split on punctuation
#work<-unlist(strsplit(work,"[[:punct:]]"))
#make all lowercase
#work<-tolower(work)
#remove blanks
#work<-work[work != ""]
#paste as single string
work.all<-paste(work, sep=" ", collapse=" ")
#set working directory to target directory
setwd(target.texts)
#write
write(work.all, file=fn.s[i])
}