R:如何根据数据框的值添加行?

R: How can I add rows based on values of a data frame?

目前,我有两个要合并的数据框。 数据框A包含每天酒店房间的能源和水消耗数据,数据框B包含关于住在房间里的人。为了匹配data frame A的数据结构,我需要解决以下关于data frame B.

的问题

数据框 B 目前看起来是这样的:

   `Person ID``                     `Apartment`                    `contract_start`         `contract_end`
   <chr>                             <chr>                          <date>                   <date>                
 1 hnd48                             T217                           2021-09-16               2021-09-18            
 2 jFDJu                             T217                           2021-09-19               2021-09-21            
 3 kqKcX                             A705                           2021-09-16               2021-09-19            

为了匹配数据框A的数据结构,每天住酒店房间的人需要是新的一行。因此,我想添加一个新列 'dates',从 'contract_start' 天开始到 'contract_end' 天结束,计算一个人住在房间里的每一天。因此,数据框理想情况下如下所示:

   `Person ID``                     `Apartment`                    `dates`         
   <chr>                             <chr>                          <date>                                 
 1 hnd48                             T217                           2021-09-16
 2 hnd48                             T217                           2021-09-17
 3 hnd48                             T217                           2021-09-18                           
 4 jFDJu                             T217                           2021-09-19
 5 jFDJu                             T217                           2021-09-20
 6 jFDJu                             T217                           2021-09-21                            
 7 kqKcX                             A705                           2021-09-16
 8 kqKcX                             A705                           2021-09-17
 9 kqKcX                             A705                           2021-09-18
 10kqKcX                             A705                           2021-09-19                             

我如何用代码做到这一点?

此致, 文森特

library(tidyverse)
df = tribble(
  ~`Person ID`, ~Apartment, ~contract_start, ~contract_end,
  "hnd48", "T217", "2021-09-16", "2021-09-18",
  "jFDJu", "T217", "2021-09-19", "2021-09-21", 
  "kqKcX", "A705", "2021-09-16", "2021-09-19"
) %>%
  mutate(across(c(contract_start, contract_end), as.Date)) 

df %>% 
  rowwise() %>% 
  mutate(
    dates = paste0(
      as.character(
        seq(contract_start, contract_end, by = "days")
        ), collapse = ",")
    ) %>%
  select(-c(contract_start, contract_end)) %>% 
  separate_rows(dates, sep = ",") %>% 
  mutate(dates = as.Date(dates))
# A tibble: 10 x 3
   `Person ID` Apartment dates     
   <chr>       <chr>     <date>    
 1 hnd48       T217      2021-09-16
 2 hnd48       T217      2021-09-17
 3 hnd48       T217      2021-09-18
 4 jFDJu       T217      2021-09-19
 5 jFDJu       T217      2021-09-20
 6 jFDJu       T217      2021-09-21
 7 kqKcX       A705      2021-09-16
 8 kqKcX       A705      2021-09-17
 9 kqKcX       A705      2021-09-18
10 kqKcX       A705      2021-09-19