问题
I want to add elements to an empty list on the fly. Each element in the list should be named automatically after a set of variables which value will vary.
However, I cannot seem to find a way to name list elements on the fly without getting errors. Consider the example below:
L <- list()
var1 <- "wood"
var2 <- 1.0
var3 <- "z4"
varname <- paste(var1, as.character(var2), var3, sep="_")
# This works fine:
L$"wood_1_z4" <- c(0,1)
L$"wood_1_z4"
0 1
# This doesn't!!
L$paste(var1, as.character(var2), var3, sep="_") <- c(0,1)
Error in L$paste(var1, as.character(var2), var3, sep = "_") <- c(0, 1) :
invalid function in complex assignment
# Ths doesn't either ...
L$eval(parse(text = "varname")) <- c(0,1)
Error in L$eval(parse(text = "varname")) <- c(0, 1) :
target of assignment expands to non-language object
Is there a way to do this?
回答1:
You cannot assign to paste()
using the <-
operator (and I believe this is true for the eval()
function as well). Instead, you need to either use the [[
operator or use the names()
function. You can do this like so:
L <- list()
var1 <- "wood"
var2 <- 1.0
var3 <- "z4"
varname <- paste(var1, as.character(var2), var3, sep="_")
# Using [[
L[[varname]] <- c(0,1)
> L
$wood_1_z4
[1] 0 1
# Using names()
L[[1]] <- c(0,1)
names(L)[1] <- varname
> L
$wood_1_z4
[1] 0 1
A more effective way to do this might be to use a function that both creates the value and names the list element, or even one that just creates the value - if you then use sapply
you can name each list element using arguments from the call to your function and the USE.NAMES
options.
In a general sense, R isn't really well-optimized for growing lists over time when the final size and structure of the list aren't well-known. While it can be done, a more "R-ish" way to do it would be to figure out the structure ahead of time.
来源:https://stackoverflow.com/questions/30351866/name-list-elements-based-on-variable-names-r