Peng Yu
2009-Oct-24 16:02 UTC
[R] 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"
Jorge Ivan Velez
2009-Oct-24 16:07 UTC
[R] How apply a function to a list while maintaining list name?
Hi Peng, Try lapply(L, paste, collapse="") $x [1] "ab" $y [1] "ab" HTH, Jorge On Sat, Oct 24, 2009 at 12:02 PM, Peng Yu <> wrote:> 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" > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Reasonably Related Threads
- How to convert a frame to a matrix while maintaining that a number is still a number?
- How to pop up the graphics window from Rscript?
- Is there a summary on different version of 'apply' functions? What is the meaning of the prefixes?
- How to wait for a user response in Rscript?
- How to not to terminate read.table if the input file is empty?