简体   繁体   English

为 R 中的部分字符串匹配两个数据帧

[英]match two dataframes for partial strings in R

For example I want to match this dataframes by using variable feature and feature2 according to the corresponding string.例如,我想根据相应的字符串使用变量 feature 和 feature2 来匹配这个数据帧。

Dataframe1                        Dataframe2
    Object price feature             person   feature2
    house  20     205                Johnson   1122056
    car    50     710                Lewis     4Dl-open75
    window 40     open               Lewis     ltkgreen
    garden 100    green              Hill      111710D

Which should give me a result like this.这应该给我这样的结果。

Object price feature person 
house   20    205    Johnson
car     50    710    Hill
window  40    open   Lewis
garden  100   green  Lewis 

you can do this relatively simple by creating a column of partially matched dataframes and then just unnest that.您可以通过创建一列部分匹配的数据框然后将其取消嵌套来相对简单地完成此操作。

library(tibble)
library(dplyr)
library(tidyr)

df1 <- tibble(
  Object = c('house','car','window','garden'),
  price = c(20,50,40,100),
  feature = c('205','710','open','green')
)

df2 <- tibble(
  person = c('Johnson','Lewis','Lewis','Hill'),
  feature2 = c('1122056','4Dl-open75','ltkgreen','111710D')
)

result <- df1 %>%
  mutate(
    df2 = lapply(feature, function(x){df2 %>% filter(grepl(x, feature2))})
  ) %>%
  unnest(df2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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