【问题标题】:How to merge by date on R with a period between two years?如何按 R 上的日期与两年之间的时间合并?
【发布时间】:2021-08-09 17:20:11
【问题描述】:

所以我有了第一个数据集

Company Director Dir_Date
AB Alexander 2014
AB Justin 2020

然后我们了解到,在 2014 年至 2019 年期间,AB 的董事是 Alexander。

我的第二个数据集是这样的

Company Results Date
AB Good 2014
AB Good 2015
AB Bad 2016

每年等等。我想通过按公司和日期合并来获得这个输出:

Company Results Date Director
AB Good 2014 Alexander
AB Good 2015 Alexander
AB Bad 2016 Alexander

【问题讨论】:

  • 要么使用fuzzy_join或者tidyr::complete master然后加入
  • 我去看看,谢谢!

标签: r dataframe date merge dataset


【解决方案1】:

在扩展示例中,展示如何硬编码其他可能的年份值

df_m <- read.table(text = 'Company  Director    Dir_Date
AB  Alexander   2014
AB  Justin  2020', header = T)

df <- read.table(text = ' Company   Results Date
AB  Good    2014
AB  Good    2015
AB  Bad 2016
AB Something  2021', header = T)

library(tidyverse)
df %>% left_join(df_m %>% group_by(Company) %>% complete(Dir_Date = 2000:2021) %>%
                   fill(Director, .direction = 'down'),
                 by = c('Company' = 'Company', 'Date' = 'Dir_Date'))


  Company   Results Date  Director
1      AB      Good 2014 Alexander
2      AB      Good 2015 Alexander
3      AB       Bad 2016 Alexander
4      AB Something 2021    Justin

df_m <- read.table(text = 'Company  Director    Dir_Date
AB  Alexander   2014
AB  Justin  2020', header = T)

df <- read.table(text = ' Company   Results Date
AB  Good    2012
AB  Good    2015
AB  Bad 2016
AB Something  2021', header = T)

library(tidyverse)
df %>% left_join(df_m %>% group_by(Company) %>% complete(Dir_Date = 2000:2021) %>%
                   fill(Director, .direction = 'down'),
                 by = c('Company' = 'Company', 'Date' = 'Dir_Date'))
#>   Company   Results Date  Director
#> 1      AB      Good 2012      <NA>
#> 2      AB      Good 2015 Alexander
#> 3      AB       Bad 2016 Alexander
#> 4      AB Something 2021    Justin

reprex package (v2.0.0) 于 2021-05-20 创建


df_m <- read.table(text = 'Company  Director    Dir_Date
AB  Alexander   2014
AB  Justin  2020', header = T)

df <- read.table(text = ' Company   Results Date
AB  Good    2014
AB  Good    2015
AB  Bad 2016', header = T)

library(tidyverse)
df %>% left_join(df_m %>% group_by(Company) %>% complete(Dir_Date = seq(min(Dir_Date), max(Dir_Date), 1)) %>%
                   fill(Director, .direction = 'down'),
                 by = c('Company' = 'Company', 'Date' = 'Dir_Date'))
#>   Company Results Date  Director
#> 1      AB    Good 2014 Alexander
#> 2      AB    Good 2015 Alexander
#> 3      AB     Bad 2016 Alexander

reprex package (v2.0.0) 于 2021-05-20 创建

【讨论】:

  • 这是我得到的:seq.default(min(.$Dir_Date), max(.$Dir_Date), 1) 中的错误:'from' 必须是有限数
  • 尝试不使用.$
  • 我用 as.Date() 转换了两个日期列,但现在我收到了这条消息(无论我尝试使用什么 .$ 组合): seq.int(0, to0 - from, by) 中的错误: 'to' 必须是有限数
  • @theeconomista,你可能有一些 NA,尝试在最小值和最大值中使用 na.rm = T
  • 即使公司和结果合并得很好,我也没有更多的错误消息,但在关于导演的输出中出现了一些问题(添加了很多 NA),所以我会尝试找到在哪里它来自!
猜你喜欢
  • 2018-08-11
  • 2020-07-28
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 2016-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多