Skip to content
Prev 205096 / 398506 Next

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:
$`Name A`
[1] "A"

$`Name B`
[1] "B"
On Fri, Jan 1, 2010 at 8:21 AM, Mark Heckmann <mark.heckmann at gmx.de> wrote: