【问题标题】:Merge data with repeating rows in R将数据与R中的重复行合并
【发布时间】:2020-11-10 14:17:53
【问题描述】:

我有这两个数据框

> df1<-data.frame(day=c(monday,monday,monday,tuesday,tuesday,tuesday,wednesday,wednesday,wednesday),time=c(06:00,06:15,06:30,06:00,06:30,07:00,06:00,06:30,06:45),
>        subject= c(ENG, GER, RUS, ENG, GER, RUS, ENG, GER, RUS))
> 
> 
> df2<-data.frame(time=c(06:00,06:15,06:30,06:45,07:00))





df1
    day       time      subject
    
    monday    06:00     ENG
    monday    06:15     GER
    monday    06:30     RUS
    tuesday   06:00     ENG
    tuesday   06:30     GER
    tuesday   07:00     RUS
    wednesday 06:00     ENG
    wednesday 06:30     GER
    wednesday 06:45     RUS

df2

time

06:00
06:15
06:30
06:45
07:00

我想得到这个

 df3

 day      time     subject
 monday   06:00    ENG
 monday   06:15    GER
 monday   06:30    RUS
 monday   06:45    NA
 monday   07:00    NA
tuesday   06:00    ENG
tuesday   06:15    NA
tuesday   06:30    GER
tuesday   06:45    NA
tuesday   07:00    RUS
wednesday 06:00    ENG
wednesday 06:15    NA
wednesday 06:30    GER
wednesday 06:45    RUS
wednesday 07:00    NA

我试过了

merge(df1, df2, by = "time", all = TRUE, sort = FALSE)

但事实并非如此。

【问题讨论】:

    标签: r merge rows repeat


    【解决方案1】:

    你可以这样做:

    library(tidyverse)
    
    df1 %>% 
       nest_by(day) %>% 
       mutate(data = list(full_join(data, df2,'time'))) %>%
       unnest(data)
    
    # A tibble: 15 x 3
    # Groups:   day [3]
       day       time  subject
       <chr>     <chr> <chr>  
     1 monday    06:00 ENG    
     2 monday    06:15 GER    
     3 monday    06:30 RUS    
     4 monday    06:45 NA     
     5 monday    07:00 NA     
     6 tuesday   06:00 ENG    
     7 tuesday   06:30 GER    
     8 tuesday   07:00 RUS    
     9 tuesday   06:15 NA     
    10 tuesday   06:45 NA     
    11 wednesday 06:00 ENG    
    12 wednesday 06:30 GER    
    13 wednesday 06:45 RUS    
    14 wednesday 06:15 NA     
    15 wednesday 07:00 NA    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 2019-01-09
      • 2018-03-21
      • 2018-06-09
      • 1970-01-01
      • 2016-02-11
      • 2012-01-14
      相关资源
      最近更新 更多