Skip to content
Back to formatted view

Raw Message

Message-ID: <6A9CC2D5-C174-48D7-A280-6531333D28A1@gmail.com>
Date: 2009-02-27T03:07:10Z
From: Alexy Khrabrov
Subject: accessing and preserving list names in lapply

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