简体   繁体   English

在 R 中按组补充缺失的日期(年月)和值

[英]Refill missing date(year-month) and value by groups in R

I am trying to fill the missing date and rate.我正在尝试填写缺失的日期和费率。 For the missing date, I wish to refill yr_month between 2019 (January) and 2021 (January).对于缺少的日期,我希望在 2019 年(一月)和 2021 年(一月)之间补充 yr_month。 For the rate, I wish to refill any missing value as zero.对于费率,我希望将任何缺失值重新填充为零。

The close example I find is here - How to add only missing Dates in Dataframe but my challenge is that I have one more column by groups.我找到的最接近的例子在这里 - 如何在 Dataframe 中仅添加缺失的日期,但我的挑战是我还有一个按组的列。 It means each patient will have 2 years of data.这意味着每位患者将拥有 2 年的数据。

What's the best way I can do this on an efficient way?我可以以有效的方式做到这一点的最佳方式是什么?

# A tibble: 10 x 3
# Groups:   clinic, yr_month [10]
   clinic   yr_month   rate
   <chr>    <chr>     <dbl>
 1 patient1 2019-01  0.528 
 2 patient1 2019-04  0.528 
 3 patient1 2020-05  0.528 
 4 patient1 2021-01  1.06  
 5 patient2 2019-01  0.0671
 6 patient2 2019-02  0.436 
 7 patient2 2019-03  0.805 
 8 patient2 2019-04  0.671 
 9 patient2 2019-05  0.268 
10 patient2 2019-06  0.101 

dput输入

structure(list(clinic = c("patient1", "patient1", "patient1", 
"patient1", "patient2", "patient2", "patient2", "patient2", "patient2", 
"patient2"), yr_month = c("2019-01", "2019-04", "2020-05", "2021-01", 
"2019-01", "2019-02", "2019-03", "2019-04", "2019-05", "2019-06"
), rate = c(0.527704485488127, 0.527704485488127, 0.527704485488127, 
1.05540897097625, 0.0671163461861136, 0.436256250209739, 0.805396154233364, 
0.671163461861136, 0.268465384744455, 0.10067451927917)), row.names = c(NA, 
-10L), groups = structure(list(clinic = c("patient1", "patient1", 
"patient1", "patient1", "patient2", "patient2", "patient2", "patient2", 
"patient2", "patient2"), yr_month = c("2019-01", "2019-04", "2020-05", 
"2021-01", "2019-01", "2019-02", "2019-03", "2019-04", "2019-05", 
"2019-06"), .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
    8L, 9L, 10L), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, 10L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

Expected output:预期 output:

# Groups:   clinic, yr_month 
   clinic   yr_month   rate
   <chr>    <chr>     <dbl>
 1 patient1 2019-01      0
 1 patient1 2019-02      0 
 1 patient1 2019-03      0
 1 patient1 2019-04  0.528
 1 patient1 2019-05      0
 1 patient1 2019-06      0 
 1 patient1 2019-07      0
 1 patient1 2019-08      0 
 1 patient1 2019-09      0
...
25 patient2 2019-01  0.0671
26 patient2 2019-02  0.436 
27 patient2 2019-03  0.805 
28 patient2 2019-04  0.671 
29 patient2 2019-05  0.268 
30 patient2 2019-06  0.101 
...
48 patient2 2021-01      0  
 

Using tidyverse and zoo you could try the following.使用tidyversezoo您可以尝试以下操作。 You can use group_by to consider each patient within a clinic.您可以使用group_by来考虑诊所内的每位患者。 For your dates, you can use as.yearmon from zoo to use a year-month format.对于您的日期,您可以使用zoo中的as.yearmon来使用年月格式。 Then, using complete from tidyr you can fill in the missing rate values with 0 for missing months.然后,使用completetidyr ,您可以使用 0 填充缺失月份的缺失rate值。

library(tidyverse)
library(zoo)

df %>%
  group_by(clinic) %>%
  mutate(yr_month = as.yearmon(yr_month)) %>%
  arrange(yr_month) %>%
  complete(yr_month = seq(first(yr_month), last(yr_month), by = 1 / 12), fill = list(rate = 0)) 

first we create an empty data set for your given month year combinations:首先,我们为您给定的月份年份组合创建一个空数据集:

library(dplyr)
library(tidyr)
library(purrr)
month_df <- tibble(
  month = rep(c(1:12), 3),
  year = c(rep(2019, 12), rep(2020, 12), rep(2021, 12))
  ) %>% 
  mutate(month_ind = ifelse(nchar(month) == 1, paste0("0", month), month),
         yr_month = paste0(year, "-", month_ind)) %>% 
  select(yr_month)

After that, given df is your data:之后,给定df是您的数据:

df %>% ungroup() %>% 
  group_split(clinic) %>% 
  map(., right_join, month_df) %>% 
  map(., fill, clinic) %>% 
  bind_rows() %>% 
  arrange(clinic, yr_month) %>% 
  mutate(rate = ifelse(is.na(rate), 0 , rate))

output is: output 是:

# A tibble: 72 x 3
   clinic   yr_month  rate
   <chr>    <chr>    <dbl>
 1 patient1 2019-01  0.528
 2 patient1 2019-02  0    
 3 patient1 2019-03  0    
 4 patient1 2019-04  0.528
 5 patient1 2019-05  0    
 6 patient1 2019-06  0    
 7 patient1 2019-07  0    
 8 patient1 2019-08  0    
 9 patient1 2019-09  0    
10 patient1 2019-10  0    
# ... with 62 more rows

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

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