英文:
how to replace a dataframe with another dataframe in R
问题
if df1 look like a df2, df1 would replaced to df2
英文:
i want to replace a df1 data, with df2, which df2 is a data like df1
example
df1 <- data.frame(
name = c(
"A. MAHJUM-61365",
"A. MAHJUM-61365. MAHJUM-61365",
"A. RIZAL. AD-11002795",
"A. RIZAL. AD-11002795. RIZAL. AD-11002795",
"ABD. KADIR-60447",
"ABD. KADIR-60447ABD. KADIR-60447",
"ABD. KAHAR-62551",
"ABD. RASYID DS-11002082",
"ABDREAS APUNG @SANY",
"ABDUL AZIS @HYUNDAY",
"ABDUL AZIZ @HYUNDAI",
"ABDUL AZIZ@HYUNDAI"
))
and df2 is
df2 <- data.frame(
name = c(
"A. MAHJUM-61365",
"A. RIZAL. AD-11002795",
"ABD. KADIR-60447",
"ABD. KAHAR-62551",
"ABD. RASYID DS-11002082",
"ABDREAS APUNG @SANY",
"ABDUL AZIS @HYUNDAY"
))
if df1 look like a df2, df1 would replaced to df2
答案1
得分: 3
如它是子字符串匹配,我们可以使用 fuzzyjoin
。
library(dplyr)
library(fuzzyjoin)
regex_left_join(df1, df2, by = 'name') %>%
transmute(name = coalesce(name.y, name.x))
或者使用基于距离的方法。
stringdist_left_join(df1, df2, by = 'name') %>%
transmute(name = coalesce(name.y, name.x))
英文:
As it is substring match, we can use fuzzyjoin
library(dplyr)
library(fuzzyjoin)
regex_left_join(df1, df2, by = 'name') %>%
transmute(name = coalesce(name.y, name.x))
or use a distance based approach
stringdist_left_join(df1, df2, by = 'name') %>%
transmute(name = coalesce(name.y, name.x))
</details>
# 答案2
**得分**: 0
你可以使用 `adist` 来查找最佳匹配并替换它们。
```R
i <- max.col(-adist(df1$name, df2$name, partial=TRUE))
df1$name <- df2$name[i]
df1
# name
#1 A. MAHJUM-61365
#2 A. MAHJUM-61365
#3 A. RIZAL. AD-11002795
#4 A. RIZAL. AD-11002795
#5 ABD. KADIR-60447
#6 ABD. KADIR-60447
#7 ABD. KAHAR-62551
#8 ABD. RASYID DS-11002082
#9 ABDREAS APUNG @SANY
#10 ABDUL AZIS @HYUNDAY
#11 ABDUL AZIS @HYUNDAY
#12 ABDUL AZIS @HYUNDAY
英文:
You can use adist
to find the best match and replace them.
i <- max.col(-adist(df1$name, df2$name, partial=TRUE))
df1$name <- df2$name[i]
df1
# name
#1 A. MAHJUM-61365
#2 A. MAHJUM-61365
#3 A. RIZAL. AD-11002795
#4 A. RIZAL. AD-11002795
#5 ABD. KADIR-60447
#6 ABD. KADIR-60447
#7 ABD. KAHAR-62551
#8 ABD. RASYID DS-11002082
#9 ABDREAS APUNG @SANY
#10 ABDUL AZIS @HYUNDAY
#11 ABDUL AZIS @HYUNDAY
#12 ABDUL AZIS @HYUNDAY
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论