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

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jan 24 12:59:09 CET 2010


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:
>
> 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.
> --
> View this message in context: http://n4.nabble.com/auto-reading-in-multiple-txt-files-with-filename-as-1st-column-ID-tp1288549p1288549.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list