[R] Automatic File Reading [Broadcast]

Rolf Turner r.turner at auckland.ac.nz
Sun Jan 24 21:01:45 CET 2010


On 23/01/2010, at 4:28 PM, Brad Patrick Schneid wrote:

>
> Hi,
>
> I have many 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.
> "
> Thanks to this thred, I can read them all in automatically.
> each file is uniquely named "site_name".txt and as you can see,  
> there are no
> headers in the files
> I would like to add a new column with "site_name" repeated for each
> observation
> which corresponds to the name of the file the data came from AND  
> also merge
> the files vertically into one huge
> data file.
> I am just learning R and would greatly appreciate any suggestions.

Assuming that you are doing something like:

for(file in files) { # Where ``files'' is a vector of the names of  
files that you are reading.
	X <- read.table(file)
	....
}

you could do:

result <- list()
for(file in files) {
	X <- read.table(file)
	names(X) <- c("date","time","mung","gorp") # Give the columns  
evocative names.
	X <- cbind(site=sub(".txt","",file),X)
	result[[file]] <- X
}

result <- do.call(rbind,result)

HTH

	cheers,

		Rolf Turner

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}



More information about the R-help mailing list