Alina
Alina

Reputation: 2261

R assign list variables to existing strings

I have a vector of string.

vec = c("a","b","c")

and a list of vectors

pr[[1]] = c(1,2,3)
pr[[2]] = c(13,2,3)
pr[[3]] = c(31,27,3)

and I want to create a list, so that each variable from my vec-vector will serve as an object and it will be assigned a corresponding (index based) values from pr. so, in fact I want to to this automatically:

list("a" = pr[[1]], "b" = pr[[2]], "c"=pr[[3]])

Somehow, I am not getting how I can do that.

Upvotes: 2

Views: 298

Answers (1)

Mamoun Benghezal
Mamoun Benghezal

Reputation: 5314

You can use

(names(pr) <- vec)
[1] "a" "b" "c"
pr
# $a
# [1] 1 2 3
# $b
# [1] 13  2  3
# $c
# [1] 31 27  3

Upvotes: 3

Related Questions