Message-ID: <366c6f340910240902g39dcf02g6d22d4748789acc7@mail.gmail.com>
Date: 2009-10-24T16:02:52Z
From: Peng Yu
Subject: How apply a function to a list while maintaining list name?
I use lapply to apply a function to the list 'L'. But of course, the
list names in 'X' is not maintained. I'm wondering if there is a
function that can maintain the list names as well as apply the
function.
$ Rscript lapply.R
> L=list(x=c('a','b'), y=c('a','b'))
> L
$x
[1] "a" "b"
$y
[1] "a" "b"
> X=lapply(1:length(L),function(x){paste(L[[x]],collapse='')})
> X
[[1]]
[1] "ab"
[[2]]
[1] "ab"
> names(X)=names(L)
> X
$x
[1] "ab"
$y
[1] "ab"