Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you read multiple .txt files into R? [duplicate]

I'm using R to visualize some data all of which is in .txt format. There are a few hundred files in a directory and I want to load it all into one table, in one shot.

Any help?

EDIT:

Listing the files is not a problem. But I am having trouble going from list to content. I've tried some of the code from here, but I get a bug with this part:

all.the.data <- lapply( all.the.files,  txt  , header=TRUE) 

saying

 Error in match.fun(FUN) : object 'txt' not found 

Any snippets of code that would clarify this problem would be greatly appreciated.

like image 273
Eric Brotto Avatar asked Aug 03 '10 15:08

Eric Brotto


People also ask

Can you read TXT files in R?

R programming language can load TXT files. If you are wondering how to read TXT files in R, the most basic function you can use is the read. table function.

How do I read all files in a folder in R?

To list all files in a directory in R programming language we use list. files(). This function produces a list containing the names of files in the named directory. It returns a character vector containing the names of the files in the specified directories.


1 Answers

You can try this:

filelist = list.files(pattern = ".*.txt")  #assuming tab separated values with a header     datalist = lapply(filelist, function(x)read.table(x, header=T))   #assuming the same header/columns for all files datafr = do.call("rbind", datalist)  
like image 174
Greg Avatar answered Sep 20 '22 12:09

Greg