Skip to content

auto reading in multiple txt files with filename as 1st column "ID"

2 messages · Brad Patrick Schneid, Gabor Grothendieck

#
Hi, 
I have many .txt files which look like this: 
 
2009/02/07 12:30:10.0 ? ? ?5.0161 ? ? ?13.208 
2009/02/07 12:45:10.0 ? ? ?5.0102 ? ? ?13.350 
2009/02/07 13:00:10.0 ? ? ?5.0044 ? ? ?13.473 
.... 
.... 
.... 
2009/02/07 16:30:10.0 ? ? ?4.9366 ? ? ?13.788 
2009/02/07 16:45:10.0 ? ? ?4.9397 ? ? ?13.798 
end data. 

###I can read in all files from "my_folder" using the following code:

flist <- list.files(path=file.path("my_folder"), pattern="[.]txt$") 
flist<-flist[grep('.txt', flist)] 
myInput <- lapply(flist, read.table, header=FALSE, skip=44)

##############################################

Each file is uniquely named "site_name.txt" , and the last row of each file
contains the line: "end data." 
I would like to do the following:
1) add a new column with "site_name" repeated for each observation/row (data
files vary in the # of observations) which corresponds to the name of the
txt file 
2) remove the last line which says "end data"
3) merge the files vertically into one huge data frame ? 

if I have the text files "site_name_A.txt" and "site_name_B.txt",  I want
the end product to be a data.frame and look like this:

site_name_A 2009/02/07 12:30:10.0 ? ? ?5.0161 ? ? ?13.208 
site_name_A 2009/02/07 12:45:10.0 ? ? ?5.0102 ? ? ?13.350 
site_name_A 2009/02/07 13:00:10.0 ? ? ?5.0044 ? ? ?13.473 
.... 
.... 
.... 
site_name_B  2009/02/07 12:30:10.0 ? ? ?4.9366 ? ? ?13.788 
site_name_B  2009/02/07 12:45:10.0 ? ? ?4.9397 ? ? ?13.798 


I am just learning R and would greatly appreciate any suggestions. 

Thanks ahead of time. ?
#
Try this:

L <- lapply(flist, function(fname) {
	DF <- read.table(fname, skip = 44, comment = "e")
	transform(DF, site_name = fname)
})
allData <- do.call("rbind", L)

On Sun, Jan 24, 2010 at 2:00 AM, Brad Patrick Schneid
<bpschn01 at gmail.com> wrote: