Sometimes I'm iterating over a list where names are keys into another
data structure, e.g. a related list. Then I can't use lapply as it
does [[]] and loses the name. Then I do something like this:
do.one <- function(ldf) { # list-dataframe item
key <- names(ldf)
meat <- ldf[[1]]
mydf <- some.df[[key]] # related data structure
r.df <- cbind(meat,new.column=computed)
r <- list(xxx=r.df)
names(r) <- key
r
}
then if I operate on the list L of those ldf's not as lapply(L,...), but
res <- lapply(1:length(L),do.one)
Can this procedure be simplified so that names are preserved?
Specifically, can the xxx=..., xxx <- key part be eliminated -- how
can we have a variable on the left-hand side of list(lhs=value)?
Cheers,
Alexy
accessing and preserving list names in lapply
4 messages · Alexy Khrabrov, Romain Francois, Baptiste Auguie
res <- lapply(1:length(L),do.one)
Actually, I do res <- lapply(:length(L),function(x)do.one(L[x])) -- this is the price of needing the element's name, so I have to both make do.one extract the name and the meat separately inside, and lapply becomes ugly. Yet the obvious alternatives -- extracting the names separately, attaching them back into list elements, etc., -- are even uglier. Something pretty? :) Cheers, Alexy
Hi, This might be the trick you are looking for: http://tolstoy.newcastle.edu.au/R/e4/help/08/04/8720.html Romain
Alexy Khrabrov wrote:
res <- lapply(1:length(L),do.one)
Actually, I do res <- lapply(:length(L),function(x)do.one(L[x])) -- this is the price of needing the element's name, so I have to both make do.one extract the name and the meat separately inside, and lapply becomes ugly. Yet the obvious alternatives -- extracting the names separately, attaching them back into list elements, etc., -- are even uglier. Something pretty? :) Cheers, Alexy
Romain Francois Independent R Consultant +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr
Hi, Perhaps Hadley's plyr package can help,
library(plyr) temp <- list(x=2,y=3,x=4) llply(temp, function(x) x^2 ) $x [1] 4 $y [1] 9 $x [1] 16
baptiste
On 27 Feb 2009, at 03:07, Alexy Khrabrov wrote:
Sometimes I'm iterating over a list where names are keys into another
data structure, e.g. a related list. Then I can't use lapply as it
does [[]] and loses the name. Then I do something like this:
do.one <- function(ldf) { # list-dataframe item
key <- names(ldf)
meat <- ldf[[1]]
mydf <- some.df[[key]] # related data structure
r.df <- cbind(meat,new.column=computed)
r <- list(xxx=r.df)
names(r) <- key
r
}
then if I operate on the list L of those ldf's not as lapply(L,...),
but
res <- lapply(1:length(L),do.one)
Can this procedure be simplified so that names are preserved?
Specifically, can the xxx=..., xxx <- key part be eliminated -- how
can we have a variable on the left-hand side of list(lhs=value)?
Cheers,
Alexy
______________________________________________ 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.
_____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag