I am trying to select some specific directories and read one specific file from each directory.
Here is an example of directories and files
|-out_original
|-----result_file
|-out_20percent_ds
|-----result_file
|-out_40percent_ds
|-----result_file
|-out_60percent_ds
|-----result_file
|-out_80percent_Ds
|-----result_file
Code
setwd("/home/data/proj/")
datatype = c("20","40","60","80","original")
filenames=as.vector(c(0))
for (i in 1:length(datatype))
{
if(i <= 5){
filenames[i]=paste0("out_",datatype[i],"percent_ds/")
}
else{
filenames[i]=paste0("out_",datatype[i])
}
}
How can I save files in to variables from each directory?
Try using list.files
. If the working directory is the root of the directory you have shown, then something link this will work:
my.files <-
list.files(path = "./", pattern = "result_file",
full.names = TRUE, recursive = TRUE)
This recursively searches for files with names containing the pattern "result_file"
in the path
given. The pattern
can be any regular expression and only files which match this regular expression are returned.
You can then read your files using something like
ans <- lapply(my.files, read.table)
assuming your data is tabular.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments