[R] lapply-related question

Bartz, Kevin Kevin.Bartz at FMR.COM
Mon Sep 9 19:02:06 CEST 2002


I would just use lapply on the indices, e.g.:

lapply(1:length(lst), function(i) lst[[i]] + vec[i])

...which is essentially what you've done, except that lapply keeps track of
your "i".

Good luck.

Kevin

-----Original Message-----
From: Ott Toomet [mailto:siim at localhost.localdomain] 
Sent: Monday, September 09, 2002 12:04 PM
To: r-help at stat.math.ethz.ch
Subject: [R] lapply-related question


Dear R-gurus,

I would like to use a lapply on a kind of "bivariate" problem.  I have
a vector and a list, components of which are vectors, e.g.

vec <- c(1,2,3)
lst <- list(1, c(2,3), c(4,5,6))

I want to apply a function to each component of the list, using the
corresponding component of the vector as a parameter.  E.g. I want a
list in the form

list(lst[[1]] + vec[1], lst[[2]] + vec[2], .... )

I think this can be achieved with a cycle and probably using lapply
with a function, storing the index in an outer environment as

i <- 1
lapply(lst, FUN=function(x) {x + vec[i]; i <<- i + 1})

but are there any more cleaner solution?

Best regards,

Ott
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list