Effectively, I will illustrate the R command "choose.files" (see here for more details) from "utils" package.
What I carry out below, using this command is to read in, from the folder specified below, many files (all of which have the same extension, .txt, in this example, although that is not necessary).
This snippet does the reading in the file paths part (the difficult part):
test=choose.files(default = "C:/MyFolder/*.*", caption = "Select files",
multi = TRUE, filters = Filters[c("txt", "All"),],
index = nrow(Filters))
where "default=C:\*.*" indicates the folder from which to read the files, and that all possible files in that folder are to be displayed. In this case, the option "filer" is actually redundant, and therefore, I could just as easily have written
test=choose.files(default = "C:/MyFolder/*.*", caption = "Select files",
multi = TRUE )
instead.
Of course, having read the full path of the files in, one can simply read the said files in as below:
NFiles=length(test)
Data=vector(mode="list",NFiles)
for (i in 1:NFiles) {
Data[[i]]=read.table(test[i],header=F)
}
As simple, and powerful, as that!
No comments:
Post a Comment