Reputation: 23
I have a list:
list = c('banana', 'apple', 'orange')
I want each list item to become a empty vector, i.e., ""
Like this:
banana
apple
orange
Does anyone know how to answer?
Upvotes: 2
Views: 45
Reputation: 101064
We can do like this as well
list2env(mapply(setNames, list(list("")), lst), .GlobalEnv)
and you will see
> banana
[1] ""
> apple
[1] ""
> orange
[1] ""
Upvotes: 0
Reputation: 886948
We can use assign
for(nm in list) assign(nm, "")
-checking
> banana
[1] ""
> apple
[1] ""
> orange
[1] ""
Upvotes: 1
Reputation: 388817
You may convert the vector to a named list and use list2env
.
list2env(as.list(setNames(rep('', length(list)), list)), .GlobalEnv)
banana
#[1] ""
apple
#[1] ""
orange
#[1] ""
Upvotes: 2