簡體   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