如何在R上按日期合并,期限在两年之间



所以我有了第一个数据集

公司董事ABAlexander2014ABJustin2020

在扩展样本上,显示如何硬编码其他可能的年份值

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

创建于2021-05-20由reprex包(v2.0.0(


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

创建于2021-05-20由reprex包(v2.0.0(

最新更新