[英]Replace values in column of a dataframe when matching to column in another dataframe in R
I just read through comparable questions, but found no answering my specific problem.我只是阅读了类似的问题,但没有找到回答我的具体问题。 I have two dataframes,
我有两个数据框,
df1 <- data.frame("name" = c("11-24", "Tim", "Anna", "67-14", "A0839", "A4b", "Lisa", "Selina"))
df2 <- data.frame("abbreviation" = c("11-24", "67-14", "A0839", "A4b"),
"name" = c("Charles", "Nick", "Harry", "Lola"))
Looking like this:看起来像这样:
> df1
name
1 11-24
2 Tim
3 Anna
4 67-14
5 A0839
6 A4b
7 Lisa
8 Selina
> df2
abbreviation name
1 11-24 Charles
2 67-14 Nick
3 A0839 Harry
4 A4b Lola
I want to replace the abbreviations found in the column "name" of df1 by the matching name in df2.我想用 df2 中的匹配名称替换在 df1 的“名称”列中找到的缩写。 So that 11-24 is replaced by Charles or A4b by Lola.
因此 11-24 被 Charles 替换,或者 A4b 被 Lola 替换。
What I tried was:我尝试的是:
df1 <- df1 %>%
mutate(name = ifelse(name %in% df2$abbreviation, df2$name, name))
But this give not the result I want.但这并没有给出我想要的结果。 I want:
我想:
> df1
name
1Charles
2 Tim
3 Anna
4 Nick
5 Harry
6 Lola
7 Lisa
8 Selina
My dataframes have a different length.我的数据框有不同的长度。 I am looking for a tidyverse-solution, maybe one of you has something in mind..
我正在寻找一个 tidyverse 解决方案,也许你们中的一个人有什么想法..
This would help me a lot:) Best, Kathrin这对我有很大帮助:) 最好的,凯瑟琳
Using join and coalesce
.使用 join 和
coalesce
。
library(dplyr)
df1 %>%
left_join(df2, by = c('name' = 'abbreviation')) %>%
transmute(name = coalesce(name.y, name))
# name
#1 Charles
#2 Tim
#3 Anna
#4 Nick
#5 Harry
#6 Lola
#7 Lisa
#8 Selina
In base you can use match
to make this update join .在base中,您可以使用
match
使此更新加入。
idx <- match(df1$name, df2$abbreviation)
idxn <- which(!is.na(idx))
#idxn <- !is.na(idx) #Alternative
df1$name[idxn] <- df2$name[idx[idxn]]
df1
# name
#1 Charles
#2 Tim
#3 Anna
#4 Nick
#5 Harry
#6 Lola
#7 Lisa
#8 Selina
Base R solution:基础 R 解决方案:
idx <- match(df1$name, df2$abbreviation)
transform(df1, name = ifelse(!is.na(idx), df2$name[idx], name))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.