kenyamashida
kenyamashida

Reputation: 23

How convert list of items into empty vectors in R?

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

Answers (3)

ThomasIsCoding
ThomasIsCoding

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

akrun
akrun

Reputation: 886948

We can use assign

for(nm in list) assign(nm, "")

-checking

> banana
[1] ""
> apple
[1] ""
> orange
[1] ""

Upvotes: 1

Ronak Shah
Ronak Shah

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

Related Questions