Dear R users: Is there a way to append selectively to components of a list (if possible, loops are to be avoided)? To illustrate the point, in the example below, I would like to append 99 to vector b of the list l.
> l <- list(a=c(1), b=c(2,3), c=c(4,5,6)) > l $a [1] 1 $b [1] 2 3 $c [1] 4 5 6 As you may expect, the result should look like: > l $a [1] 1 $b [1] 2 3 99 $c [1] 4 5 6 I believe that a combination of match and lapply will do it, but, unfortunately, it seems that I cannot get it right. lapply alone will append the 99 to all vectors in l. Trying to somehow subset with match gives me the result expected but in a form independent of the original list (see below). I think that match should appear inside the lapply function but, for now, I just cannot make it work. > lapply(l, function(x, y) {x<-c(x,y)}, y=99) $a [1] 1 99 $b [1] 2 3 99 $c [1] 4 5 6 99 > lapply(l[match("b", names(l))], function(x,y) x <- c(x,y), y=99) $b [1] 2 3 99 I use R2.7.1 on a Windows machine. Thank you so much. Tudor ______________________________________________ R-help@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.