![](/img/trans.png)
[英]Which is the fastest way to compare a couple of columns between two data frames in R?
[英]Is there a way to join a couple of columns using a R function?
我在 list.x 中有列Name
,在 list.y 中有列Name
、 Suburb
。 我願意加入他們的 list.x 和 list.y。 問題是,list.x 中的Name
是,
Name
以!
分隔而在 list.y 中由,
Name
名字然后姓氏 na,e !
而在 list.y 姓氏然后名字。list.x = data.frame(
Name = c("John!Citizen","Dipayan!Banerjee","Smith!Langley!White"))
list.y = data.frame(
Name = c("White,Smith,Langley","Citizen,John","Banerjee,Dipayan"),
Suburb = c("Langley","Mars","Here"))
我想通過Product
和Molecule
將x.list
與y.list
匹配
orderAlp<-function(x){
str_split(x, ' ') %>% lapply(x, 'sort') %>% lapply(x, 'paste', collapse=' ') %>% unlist(x)
}
x.list %>% full_join(y.list,by="Product") %>%
mutate(Moleculs.x=str_replace_all(Moleculs.x,"!",","),
Molecules.y=orderAlp(Molecules.y))
我想在這里添加 output 但是 obs 名稱太長了,我無法添加它。
嘗試這個:
library(dplyr)
library(tidyr)
x.list %>%
separate_rows(Moleculs.x, sep = '!') %>%
rename(Molecules = Moleculs.x) %>%
inner_join(y.list %>%
separate_rows(Molecules.y, sep = ',') %>%
rename(Molecules = Molecules.y), by = c("Product", "Molecules")) -> result
result
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.