喂
卡茨洪德
所以我对R比较陌生,我有以下问题,我还没有找到答案。
我有两个表,其中一个是
您可以尝试下面的代码
> merge(df1,df2)[-1]
ID English
1 1 hello
2 4 dog
3 3 cat
或data.table
选项
> setDT(df1)[setDT(df2),on = .(German)][,German:=NULL][]
ID English
1: 1 hello
2: 3 cat
3: 4 dog
> dput(df1)
structure(list(ID = c(1L, 3L, 4L), German = c("Hallo", "Katze",
"Hund")), class = "data.frame", row.names = c(NA, -3L))
> dput(df2)
structure(list(English = c("hello", "cat", "dog"), German = c("Hallo",
"Katze", "Hund")), class = "data.frame", row.names = c(NA, -3L
))
从base R
中选择match
df1$English <- with(df1, df2$English[match(German, df2$German)])
df1[c("ID", "English")]
数据df1 <- structure(list(ID = c(1L, 3L, 4L), German = c("Hallo", "Katze",
"Hund")), class = "data.frame", row.names = c(NA, -3L))
df2 <- structure(list(English = c("hello", "cat", "dog"), German = c("Hallo",
"Katze", "Hund")), class = "data.frame", row.names = c(NA, -3L
))