[R] Using changing names in loop in R

Joshua Wiley jwiley.psych at gmail.com
Sat Nov 6 23:44:36 CET 2010


Hi,

If you have data that is similar enough to warrant only changing the
extension (i.e., 1, 2, etc.) and that you (at least at times) wish to
perform operations on together, it is time to start thinking of a more
flexible framework.  Fortunately, such a framework already exists in
lists.  Lists let you keep diverse data structures (i.e., you do not
have to combine everything into a simple vector).  Lists make it
tremendously easy to do many tasks (including the two you mentioned).
For example, suppose I want to read in 10 files and then do some
manipulations:

## initialize an empty list
dat <- vector(mode = "list", length = 10)

for(i in 1:10) {
  dat[[i]] <- read.table(paste(myfilename, i, sep = ''), header = TRUE, etc.)
}

# presumably these are data frames at this point

Now everything is nicely stored, dat[[1]] is even intuitively similar
to Data_1, Data_2, etc.  At this point, suppose I want to create some
new stuff:

dat[[11]] <- matrix(1, ncol = 1, nrow = 5) # add in a matrix

for(i in 1:n) {
  dat[[i]] <- dat[[i]] + dat[[i - 1]]
}

dat[[12]] <- 5 # just a little vector

Another great advantage of lists is that it is possible to name their
elements.  This can make things more meaningful or aid the memory.
However, even when an element is named, it can still be accessed by
its index

# name the first element 'price'
names(dat)[[1]] <- "price"

now I could access it as any of these:

dat$price
dat[["price"]]
dat[[1]]

If that weren't enough, you can easily use functions on every element
of a list with constructs such as lapply(), no for loop required!

lapply(X = dat, FUN = mean, na.rm = TRUE)

It is possible to not use lists and still do what you are after, but
frankly it is messier, more prone to error, and less effective in many
cases.  It is generally a very nice feature of the assignment operator
that it is aware of its environment and does not go assigning or
overwriting things where you do not expect.  You're left to the wolves
and your own wits with assign().

HTH,

Josh

On Sat, Nov 6, 2010 at 9:22 AM, Tuatara <franziskabroell at gmail.com> wrote:
>
> Hello everybody,
>
> I have usually solved this problem by repeating lines of codes instead of a
> loop, but it's such a waste of time, I thought I should really learn how to
> do it with loops:
>
> What I want to do:
>
> Say, I have several data files that differ only in a number, e.g. data
> points (or vector, or matrix...) Data_1, Data_2, Data_3,... and I want to
> manipulate them
>
> e.g. a simple sum of several data points
>
>>data <- c(NA,n)
>>for (i in 1:n){
>>data[i] <- Data_i + Data_[i-1]
>>                  }
>
> I know that the above code doesn't work, and I don't want to combine the
> files into one vector to solve the problem etc. - I would just like to know
> who make sure R recognizes the extension "_i". I have the same problem for
> say, reading in datafiles that only differ by one digit in the extension,
> and I want to (instead of repeating code) combine the process in a loop.
>
> I hope I made myself clear to what my problem is.
>
> Thanks for your help,
>
> //F
> --
> View this message in context: http://r.789695.n4.nabble.com/Using-changing-names-in-loop-in-R-tp3030132p3030132.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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list