[R] changing a list element's name during execution in lapply - possible?
Happy New Year, all! I want to do calculations on each element of a list l, but i want the returned list element to be named differently after the calculation. Is it possible to do the renaming somehow within the lapply call? l - list(a=NA, b=NA) lapply(l, function(x) {names(x) - new name; return(x) }) This does not work, any ideas? TIA ––– Mark Heckmann Dipl. Wirt.-Ing. cand. Psych. Vorstraße 93 B01 28359 Bremen Blog: www.markheckmann.de R-Blog: http://ryouready.wordpress.com __ 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.
Re: [R] changing a list element's name during execution in lapply - possible?
On Jan 1, 2010, at 8:21 AM, Mark Heckmann wrote: Happy New Year, all! I want to do calculations on each element of a list l, but i want the returned list element to be named differently after the calculation. Is it possible to do the renaming somehow within the lapply call? l - list(a=NA, b=NA) lapply(l, function(x) {names(x) - new name; return(x) }) This does not work, any ideas? It did work in the sense that it created a list of the same structure with each node now named new name. If you want that named list to become l, then you need to do an assignment: l - lapply(l, function(x) {names(x) - new name; return(x) }) l $a new name NA $b new name NA -- David. TIA ––– Mark Heckmann Dipl. Wirt.-Ing. cand. Psych. Vorstraße 93 B01 __ 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.
Re: [R] changing a list element's name during execution in lapply - possible?
Hi David, thanks for the answer, though I am not sure what you mean. My aim is the following out: input to lapply l $a [1] NA $b [1] NA output from lapply: l $new_b [1] NA $new_b [1] NA Mark Am 01.01.2010 um 14:58 schrieb David Winsemius: On Jan 1, 2010, at 8:21 AM, Mark Heckmann wrote: Happy New Year, all! I want to do calculations on each element of a list l, but i want the returned list element to be named differently after the calculation. Is it possible to do the renaming somehow within the lapply call? l - list(a=NA, b=NA) lapply(l, function(x) {names(x) - new name; return(x) }) This does not work, any ideas? It did work in the sense that it created a list of the same structure with each node now named new name. If you want that named list to become l, then you need to do an assignment: l - lapply(l, function(x) {names(x) - new name; return(x) }) l $a new name NA $b new name NA -- David. TIA Mark Heckmann Dipl. Wirt.-Ing. cand. Psych. Vorstraße 93 B01 Mark Heckmann Dipl. Wirt.-Ing. cand. Psych. Vorstraße 93 B01 28359 Bremen Blog: www.markheckmann.de R-Blog: http://ryouready.wordpress.com [[alternative HTML version deleted]] __ 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.
Re: [R] changing a list element's name during execution in lapply - possible?
Is this what you want: l - list(a=NA, b=NA) names(l) - paste(new_, names(l), sep='') l $new_a [1] NA $new_b [1] NA On Fri, Jan 1, 2010 at 9:48 AM, Mark Heckmann mark.heckm...@gmx.de wrote: Hi David, thanks for the answer, though I am not sure what you mean. My aim is the following out: input to lapply l $a [1] NA $b [1] NA output from lapply: l $new_b [1] NA $new_b [1] NA Mark Am 01.01.2010 um 14:58 schrieb David Winsemius: On Jan 1, 2010, at 8:21 AM, Mark Heckmann wrote: Happy New Year, all! I want to do calculations on each element of a list l, but i want the returned list element to be named differently after the calculation. Is it possible to do the renaming somehow within the lapply call? l - list(a=NA, b=NA) lapply(l, function(x) {names(x) - new name; return(x) }) This does not work, any ideas? It did work in the sense that it created a list of the same structure with each node now named new name. If you want that named list to become l, then you need to do an assignment: l - lapply(l, function(x) {names(x) - new name; return(x) }) l $a new name NA $b new name NA -- David. TIA Mark Heckmann Dipl. Wirt.-Ing. cand. Psych. Vorstraße 93 B01 Mark Heckmann Dipl. Wirt.-Ing. cand. Psych. Vorstraße 93 B01 28359 Bremen Blog: www.markheckmann.de R-Blog: http://ryouready.wordpress.com [[alternative HTML version deleted]] __ 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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]] __ 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.
Re: [R] changing a list element's name during execution in lapply - possible?
Try this: # same args as lapply. FUN must return named components. lapplyWithRename - function(...) { x - lapply(...) names(x) - sapply(x, names) lapply(x, function(x) { names(x) - NULL; x }) } # test function - if x is A then f returns c(Name A = A) f - function(x) structure(x, .Names = paste(Name, x)) L - list(a=A, b=B) lapplyWithRename(L, f) Output looks like this: lapplyWithRename(L, f) $`Name A` [1] A $`Name B` [1] B On Fri, Jan 1, 2010 at 8:21 AM, Mark Heckmann mark.heckm...@gmx.de wrote: Happy New Year, all! I want to do calculations on each element of a list l, but i want the returned list element to be named differently after the calculation. Is it possible to do the renaming somehow within the lapply call? l - list(a=NA, b=NA) lapply(l, function(x) {names(x) - new name; return(x) }) This does not work, any ideas? TIA ––– Mark Heckmann Dipl. Wirt.-Ing. cand. Psych. Vorstraße 93 B01 28359 Bremen Blog: www.markheckmann.de R-Blog: http://ryouready.wordpress.com __ 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. __ 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.
Re: [R] changing a list element's name during execution in lapply - possible?
Thanks Gabor, this is what I was looking for. Mark Am 01.01.2010 um 16:05 schrieb Gabor Grothendieck: Try this: # same args as lapply. FUN must return named components. lapplyWithRename - function(...) { x - lapply(...) names(x) - sapply(x, names) lapply(x, function(x) { names(x) - NULL; x }) } # test function - if x is A then f returns c(Name A = A) f - function(x) structure(x, .Names = paste(Name, x)) L - list(a=A, b=B) lapplyWithRename(L, f) Output looks like this: lapplyWithRename(L, f) $`Name A` [1] A $`Name B` [1] B On Fri, Jan 1, 2010 at 8:21 AM, Mark Heckmann mark.heckm...@gmx.de wrote: Happy New Year, all! I want to do calculations on each element of a list l, but i want the returned list element to be named differently after the calculation. Is it possible to do the renaming somehow within the lapply call? l - list(a=NA, b=NA) lapply(l, function(x) {names(x) - new name; return(x) }) This does not work, any ideas? TIA Mark Heckmann Dipl. Wirt.-Ing. cand. Psych. Vorstraße 93 B01 28359 Bremen Blog: www.markheckmann.de R-Blog: http://ryouready.wordpress.com __ 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. Mark Heckmann Dipl. Wirt.-Ing. cand. Psych. Vorstraße 93 B01 28359 Bremen Blog: www.markheckmann.de R-Blog: http://ryouready.wordpress.com [[alternative HTML version deleted]] __ 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.