[R] basic programming question

Don MacQueen macq at llnl.gov
Thu Apr 15 21:51:48 CEST 2004


There are indeed a number of ways to do this.

If all have exactly the same number of values, and they have the same 
times, then a matrix or dataframe would be a reasonable place to 
store them. In that case I would have one column per file.

Otherwise, I would tend to use a list.

Here is a toy illustration.

>  d <- list()
>  d[[1]] <- 1:10
>  d[[2]] <- 1:20
>  names(d) <- paste('file',1:2,sep='')
>  d
$file1
  [1]  1  2  3  4  5  6  7  8  9 10

$file2
  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

>  plot(d$file1)   # or whatever makes sense to do with the data from one file

For your situation, the loop would look something like this.

lf<-list.files(pt)
d <- list()
for (i in seq(lf)) {
   d[[i]] <- scan(paste(pt,lf[i],sep="/"),skip=12)
}
names(d) <- lf

Then the i'th element of d is the data from the i'th file in your 
list of files.

If it makes sense, you can do things like
   d[[5]] - d[[2]
to subtract the values in the 2nd file from those in the 5th file, for example.

-Don

At 8:08 AM -0700 4/15/04, Randy Zelick wrote:
>Hello list,
>
>I am just starting to write some R functions (R 1.8 and Windows XP) and
>got stuck as described below:
>
>We have a bunch of time series data files, each is about 10,000 values.
>There is a 12-line header in each one. I can read them and plot them
>easily with R, but I want to make an automated system to read all the
>files in a folder and have them appear as R objects that I can assemble in
>different plots, or analyze together.
>
>Perhaps this would be best done reading into a data table, where each row
>is one of the files and each row extends for 10,000 columns? Or perhaps as
>a matrix? I could use advice on that. My first notion, though, was just to
>have them be individual files.
>
>I started as follows:
>
>#
># build a path from its parts:
>#
>p1="C:"
>p2="Work-PSU"
>p3="Lab-People-and-Projects"
>p4="Dane"
>p5="2004-04-06"
>pt=file.path(p1,p2,p3,p4,p5,fsep="/")
>#
>lf<-list.files(pt)
>ff="files"
>sayfiles=paste(length(lf),ff,sep=" ")
>#
># at this point if I type "sayfiles" I get "30 files" printed at the
>console. OK, this is progress.
>#
># Now I want to cycle through all the files and read in each one. The
># code here almost works, but I don't know how to do in R the simple task
># of substituting the read file name as the data object name.
>#
>d=dir(pt)
>for (i in d){d[i]=scan(paste(pt,i,sep="/"),skip=12)} # this almost works
>#
>
>I know this is wrong, I just want to show my intention -- that is on each
>iteration of the loop have a new R object made called d[i], the name of
>the current file. This new object is then the result of the scan operation
>on the file of that same name.
>
>Thanks,
>
>=Randy=
>
>R. Zelick				email: zelickr at pdx.edu
>Department of Biology			voice: 503-725-3086
>Portland State University		fax:   503-725-3888
>
>mailing:
>P.O. Box 751
>Portland, OR 97207
>
>shipping:
>1719 SW 10th Ave, Room 246
>Portland, OR 97201
>
>
>R. Zelick				email: zelickr at pdx.edu
>Department of Biology			voice: 503-725-3086
>Portland State University		fax:   503-725-3888
>
>mailing:
>P.O. Box 751
>Portland, OR 97207
>
>shipping:
>1719 SW 10th Ave, Room 246
>Portland, OR 97201
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


-- 
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA




More information about the R-help mailing list