使用grep通过多列连接数据帧

时间:2018-02-21 15:34:07

标签: r join grep dplyr grepl

我想基于2列完成两个数据帧的完全连接,其中1列包含在另一列中找到的字符串。以下是我的两个数据框:

date<-as.Date(c('2010-11-1','2008-3-25','2007-3-14'))
site<-c("abcejams.com", "reitimes.com", "posehbc")
desc1<-c("alpha", "beta", "gamma"
df1<-data.frame(date, site, desc1)
df1

        date         site    desc1
1 2010-11-01 abcejams.com    alpha
2 2008-03-25 reitimes.com     beta
3 2007-03-14      posehbc    gamma

date2<-as.Date(c('2010-11-1','2008-3-25','2007-3-14', '2018-2-9'))
site2<-c("jams", "time", "pose", "abce")
metric2<-c(1,2,3,4)
metric3<-c(10,20,30,40)
df2<-data.frame(date2, site2, metric2, metric3)
df2

       date2 site2 metric2 metric3
1 2010-11-01  jams       1      10
2 2008-03-25  time       2      20
3 2007-03-14  pose       3      30
4 2018-02-09  abce       4      40

我希望通过日期和站点加入此项,基于站点2在站点中的日期。如果没有grep部分,通常会这样做。

finaldf<-full_join(df1, df2, by = c("date"="date2", "site" = "site2"))

有一种方法可以使用sqldf执行此操作,但唯一的选择是左连接而不是完整连接:

test<-sqldf("df1.*, df2.metric2, 
df2.metric3 
        from df1 
        left join df2 
        on 
        instr(df1.site,  df2.site2)
        and 
        df1.date=df2.date2")

目标是让最终输出看起来像这样:

        date         site     desc1     metric2    metric3
1 2010-11-01 abcejams.com     alpha           1         10 
2 2008-03-25 reitimes.com      beta           2         20
3 2007-03-14      posehbc     gamma           3         30
4 2018-02-09         abce        NA           4         40

任何人都有这方面的经验吗?

2 个答案:

答案 0 :(得分:2)

您可以使用fuzzyjoin包并使用regex_full_join。我现在不相信CRAN,所以请检查github page进行安装。

library(fuzzyjoin)

date <- as.Date(c('2010-11-1', '2008-3-25', '2007-3-14'))
site <- c("abcejams.com", "reitimes.com", "posehbc")

df1 <- data.frame(date, site, stringsAsFactors = FALSE)

date2 <- as.Date(c('2010-11-1', '2008-3-25', '2007-3-14', '2018-2-9'))
site2 <- c("jams", "time", "pose", "abce")
metric2 <- c(1, 2, 3, 4)
metric3 <- c(10, 20, 30, 40)

df2 <- data.frame(date2, site2, metric2, metric3, stringsAsFactors = FALSE)

regex_full_join(df1, df2, by = c("site" = "site2", "date" = "date2"))


            date         site      date2 site2 metric2 metric3
1 2010-11-01 abcejams.com 2010-11-01  jams       1      10
2 2008-03-25 reitimes.com 2008-03-25  time       2      20
3 2007-03-14      posehbc 2007-03-14  pose       3      30
4       <NA>         <NA> 2018-02-09  abce       4      40

答案 1 :(得分:0)

# original data
date<-as.Date(c('2010-11-1','2008-3-25','2007-3-14'))
site<-c("abcejams.com", "reitimes.com", "posehbc")
desc1<-c("alpha", "beta", "gamma")
df1<-data.frame(date, site, desc1)

date2<-as.Date(c('2010-11-1','2008-3-25','2007-3-14', '2018-2-9'))
site2<-c("jams", "time", "pose", "abce")
metric2<-c(1,2,3,4)
metric3<-c(10,20,30,40)
df2<-data.frame(date2, site2, metric2, metric3)

我编写了一个函数,它根据TRUE这两个条件定义组。

library(tidyverse)
library(stringr)

make_groups <- function(bicond) {
    lapply(apply(bicond, 1, function(i) which(i == 1)), function(i) if (length(i)==0) { NA } else { i })
}

custom_join <- function(df1, df2) {
    cond1 <- outer(df2$date2, df1$date, "==")
    cond2 <- outer(as.character(df2$site2), as.character(df1$site), function(i, j) str_detect(j, i))
    bicond <- cond1 * cond2
    data1 <- df1 %>% mutate(G = row_number())
    data2 <- df2 %>% mutate(G = make_groups(bicond)) %>% unnest(G)
    full_join(data2, data1, by=c("G" = "G", "date2" = "date")) %>% select(-G)
}

custom_join(df1, df2)

       # date2 site2 metric2 metric3         site desc1
# 1 2010-11-01  jams       1      10 abcejams.com alpha
# 2 2008-03-25  time       2      20 reitimes.com  beta
# 3 2007-03-14  pose       3      30      posehbc gamma
# 4 2018-02-09  abce       4      40         <NA>  <NA>

另一个例子

# new data
date<-as.Date(c('2010-11-1','2008-3-25','2007-3-14','2007-3-14'))
site<-c("abcejams.com", "reitimes.com", "posehbc", "poseur")
desc1<-c("alpha", "beta", "gamma", "epsilon")
df1<-data.frame(date, site, desc1)

date2<-as.Date(c('2010-11-1','2008-3-25','2007-3-14', '2007-2-9'))
site2<-c("jams", "time", "pose", "abce")
metric2<-c(1,2,3,4)
metric3<-c(10,20,30,40)
df2<-data.frame(date2, site2, metric2, metric3)

custom_join(df1, df2)

       # date2 site2 metric2 metric3         site   desc1
# 1 2010-11-01  jams       1      10 abcejams.com   alpha
# 2 2008-03-25  time       2      20 reitimes.com    beta
# 3 2007-03-14  pose       3      30      posehbc   gamma
# 4 2007-03-14  pose       3      30       poseur epsilon
# 5 2007-02-09  abce       4      40         <NA>    <NA>