繁体   English   中英

有没有办法使用 R function 加入几列?

[英]Is there a way to join a couple of columns using a R function?

我在 list.x 中有列Name ,在 list.y 中有列NameSuburb 我愿意加入他们的 list.x 和 list.y。 问题是,list.x 中的Name是,

  1. list.x 中的Name!分隔而在 list.y 中由,
  2. list.x 中的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"))

我想通过ProductMoleculex.listy.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM