[R] importing multiple file form folder

Rui Barradas ruipbarradas at sapo.pt
Mon May 28 23:11:59 CEST 2012


Hello,

I've named your file 'file1.txt' and with

readLines("file1.txt")

saw 25 lines, then a header, then a table of tab separated values.
The header is full of blanks, such as the ones in 'length (m)' and 
'temperature (°C)', making it impratical.

So if 'flist' is your list of files, try the following.


flist <- "file1.txt"
# First, read only one and keep only the lengths column
Length <- read.table(flist[1], skip=26)[, 1]
# Then read the temperatures from all files and cbind them into a matrix
Temp <- do.call(cbind, lapply(flist, function(x) read.table(x, 
skip=26)[, 2]))
# Tidy up
colnames(Temp) <- paste("Temperature", seq_len(ncol(Temp)), sep=".")
# And put it all together
result <- cbind(Length=Length, Temp)


Hope this helps,

Rui Barradas

Em 28-05-2012 21:02, mpavlic escreveu:
> Hi all,
>
> I have a set of files (which is growing) in a folder. The files are text
> files...
> The form of files is such :
>
>
> ...with numbers for Length (m) going up to 2000 ...
> Anyway...i just need the data from first two columns  (length (m) and
> Temperature (C)), and no data before that...
>
> This Lenght (m) values are always the same. My final dataset should lokk
> like this :
>   column 1 as Length(m) ; column 2 as Temperature from first file ; column3
> as temperature from second file...and so on...
>
> I know how to import this manualy, but can seem to find a way to automate
> it...the problem is that the amout of files will be growing for quite quite
> some time, so automation is necessary.
>
> Any help is greatly apreciated.
> m
>
> --
> View this message in context: http://r.789695.n4.nabble.com/importing-multiple-file-form-folder-tp4631637.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