批量替换df1中df2的值
代码如下:
df1 <- data.frame(names= sample(LETTERS[1:10]), duration=sample(0:100, 10))
>df1
names duration
1 H 86
2 D 70
3 A 66
4 F 28
5 C 88
6 G 17
7 I 91
8 E 82
9 J 19
10 B 7
df2 <- data.frame(names= LETTERS[1:5], names_new=letters[1:5])
>df2
names names_new
1 A a
2 B b
3 C c
4 D d
5 E e
df1[match(df2$names, df1$names), "names"] <- df2$names_new
>df1
names duration
1 H 86
2 d 70
3 a 66
4 F 28
5 c 88
6 G 17
7 I 91
8 e 82
9 J 19
10 b 7