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?
[[
:L[[paste(var1, as.character(var2), var3, sep="_")]]<-c(0,1)
– Flexurenames(L)
? – Rollick$
doesn't do any evaluation on the right hand side, hence the errors. – Wisdom