Reputation: 147
I have a df like this:
df.temp1 = data.frame("A"=c(2,3,4,6,8,2,5,7))
> df.temp1
A
1 2
2 3
3 4
4 6
5 8
6 2
7 5
8 7
And a vector like this:
vec_list = c(10,20,90,40,60,70,80,100)
I need a value of list assigned to a new column based index of vec_list. The desired output is:
A B
1 2 20
2 3 90
3 4 40
4 6 70
5 8 100
6 2 20
7 5 60
8 7 80
How to do it? I tried melt
, but got errors.
Upvotes: 1
Views: 351
Reputation: 887008
We can make use of the 'A' column as index for subsetting the 'vec_list' and assign it to 'B'
df.temp1$B <- vec_list[df.temp1$A]
df.temp1
# A B
#1 2 20
#2 3 90
#3 4 40
#4 6 70
#5 8 100
#6 2 20
#7 5 60
#8 7 80
Upvotes: 2