简体   繁体   中英

How can I read multiple files in R

I have so many file(around 600) with these names:

x2008_1_3.txt
x2008_1_4.txt
x2008_1_5.txt
x2008_1_6.txt
x2008_1_7.txt
x2008_1_8.txt
.
.
.
.
x2009_1_3.txt
x2009_1_4.txt
x2009_1_5.txt
x2009_1_6.txt
x2009_1_7.txt
x2009_1_8.txt
.
.
.
.

I try so many ways to inter them as my infile all of them togather in R. But i still cannot have them all. i also want to make the output'names have the same name as input. any suggestion?

You can set the files pattern to list.files to get a list of the files:

list.files(path,pattern="^x[0-9]{4}_1_[0-9][.]txt",full.names = TRUE)

Set recursive=TRUE if your files in different directories.

I am not the best at R but this may help it is a version of a script i use for something similar using CSV's

Set dir, remember to use double \\

directory = "Location of files you want imported" #IE c:\\Folder1\\Folder2
files = list.files(path=directory,pattern = "[.]txt") #Make a list of files, assuming you want all files in that folder

for(i in 1:length(files)) # loop though all files and use assign to create data frames or replace with a different function like read.csv or append ect..
{
  file = files[i]
  assign(file,read.table(paste(directory,file,sep = "\\"),sep="\t"))
}

I hope this helps a little!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM