Robert Gentleman
2002-Sep-09 16:27 UTC
[ripley@stats.ox.ac.uk: Re: [R] lapply-related question]
Forwarded to r-devel since it is a slight change of topic - it seems that it might be worth considering putting two special variables into the lapply/sapply environment, .currentIndex and .currentTag - these are both hard to get right now, and the solutions that people are finding, seem to depend on the order in which the lapply is applied (that might change -- also, if we get some form of parallelism going this solution should be nicer). ----- Forwarded message from Prof Brian D Ripley <ripley@stats.ox.ac.uk> ----- Date: Mon, 9 Sep 2002 17:17:20 +0100 (BST) From: Prof Brian D Ripley <ripley@stats.ox.ac.uk> To: Ott Toomet <siim@localhost.localdomain> cc: r-help@stat.math.ethz.ch Subject: Re: [R] lapply-related question In-Reply-To: <200209091604.g89G4EX08235@localhost.localdomain> Precedence: SfS-bulk On Mon, 9 Sep 2002, Ott Toomet wrote:> 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?Use lapply(seq(along=lst). function(i, vec, lst){body}, vec=vec, lst=lst) using the index i in the body. Or just use a for loop. -- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ ----- End forwarded message ----- -- +---------------------------------------------------------------------------+ | Robert Gentleman phone : (617) 632-5250 | | Associate Professor fax: (617) 632-2444 | | Department of Biostatistics office: M1B20 | Harvard School of Public Health email: rgentlem@jimmy.dfci.harvard.edu | +---------------------------------------------------------------------------+ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel 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-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._