r语言 - 用另一个表中的"translation"替换值



所以我对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
))

相关内容

  • 没有找到相关文章

最新更新