I am attempting to create an environment to store information for each row of a CSV file. I need some help in making a loop assigning the variable name in the new environment to be the same as the value of a specific column. I don't care as much about what its value is as much as what it is called (I'll modify the value later). How would I go about doing this?
ex., lets say that in csvFile$Names, the value is 'Blargh'. I want to make a loop that would create a variable dataEnv$Blargh which holds ANYTHING as its value.
What I have so far which is definitely wrong:
dataEnv <- new.env()
for (value in csvFile$Names) {
dataEnv$Names <- c(Names)
}
If I understand correctly you have a vector of character strings, Names
, and want to create an environment e
containing objects corresponding to each element of Names
having that element as its name and containing an arbitrary value. Using NULL as that value we have:
Names <- c("Blarg", "foo")
e <- new.env()
for(nm in Names) e[[nm]] <- NULL
or in one line:
e <- list2env(Map(function(x) NULL, Names))
or using the components of Names
for both the names and the values:
e <- list2env(as.list(setNames(Names, Names)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.