YOLO
YOLO

Reputation: 21749

Split a list in R into new vectors

My question looks quite simple but I can't get my head around it.

I have a list:

f <- list(a = c(1,2,3), b = c('x','y','z'), c = c(0.1,0.2,0.3))

I want to split this list such that I get three new vectors in my environment where:

a <- c(1,2,3)
b <- c('x','y','z')
c <- c(0.1,0.2,0.3)

So that when I do print(a) I should get c(1,2,3) as its value and so on.

Upvotes: 0

Views: 144

Answers (3)

Jos&#233;
Jos&#233;

Reputation: 931

For me, this is the best way:

list2env(f,.GlobalEnv)

Upvotes: 1

Manuel Bickel
Manuel Bickel

Reputation: 2206

Another option would be

for (i in names(f)) {
  assign(i, f[[i]])
}

Your original list will still exist in the environment. You may or may not want to remove it.

Upvotes: 1

Zheyuan Li
Zheyuan Li

Reputation: 73415

Just use attach(f). But remember to do detach later.

Upvotes: 1

Related Questions