Automatically assign object names when importing multiple files into R

So, I uploaded a dataset containing 900 txt files, one for each biological sample. I want to combine all this data into one data matrix in R.

txt_files = list.files() # read txt files into a list for (i in length(txt_files)){ x <- read.table(file=txt_files[i], sep="\t", header=TRUE, row.name=1) } 

All files are in the same folder, so I use list.files() to query all file names. Then I want to read each table into a separate R-object (which in this case is called x). The problem is that I would like to name each object after the name of the actual file instead of x.

I tried a couple of things and tried to search the Internet, but have not yet found a solution. One thing I found is to use lapply to import them all into a data list.

 data_list = lapply(txt_files, read.table, sep = "\t") 

However, I don’t think it would be appropriate for me, since after that the data matrices are no longer available. I hope someone can help me.

+4
source share
3 answers

Naming related (especially sequential) things is generally bad. The next thing you need to do is loop over these things, which means you need to create names by inserting bits together. This is a mess.

Keep things listed whenever possible. You did it. I created several CSV files:

 > txt_files=c("f1.txt","f2.txt","f3.txt","f4.txt","f5.txt") > data_list = lapply(txt_files, read.table, sep = ",") > data_list[[1]] V1 V2 V3 1 1 2 3 > data_list[[3]] V1 V2 V3 1 1 2 3 2 5 4 3 3 1 2 3 

So now I can iterate over them with for(i in 1:length(txt_files)) and get the file name with txt_files[i] and so on:

 > for(i in 1:length(txt_files)){ + cat("File is ",txt_files[i],"\n") + print(summary(data_list[[i]])) + } File is f1.txt V1 V2 V3 Min. :1 Min. :2 Min. :3 1st Qu.:1 1st Qu.:2 1st Qu.:3 Median :1 Median :2 Median :3 Mean :1 Mean :2 Mean :3 3rd Qu.:1 3rd Qu.:2 3rd Qu.:3 Max. :1 Max. :2 Max. :3 File is f2.txt V1 V2 V3 Min. :1 Min. :2 Min. :3 1st Qu.:1 1st Qu.:2 1st Qu.:3 Median :1 Median :2 Median :3 Mean :1 Mean :2 Mean :3 3rd Qu.:1 3rd Qu.:2 3rd Qu.:3 Max. :1 Max. :2 Max. :3 ... 

[etc.]

+6
source

You can do something like this:

 names(data_list) <- txt_files 

Or perhaps:

 names(data_list) <- basename(txt_files) 

Or maybe use sapply instead of lapply .

+3
source

Without viewing your data, assign can be applied as follows:

 txt_files = list.files() # read txt files into a list for (i in length(txt_files)){ x <- read.table(file=txt_files[i], sep="\t", header=TRUE, row.name=1) assign(paste(txt_files[i], "name", sep="."), x) } 

You can also use get to call each created object:

 x <- get(paste(txt_files[i], "name", sep=".")) 
+1
source

All Articles