简体   繁体   English

R:宽到长和填充日期

[英]R: wide to long and fill dates

week = seq(as.Date("2022-9-1"), as.Date("2022-10-1"), by="weeks")
visits = c("[1,2,6,4,6,11,19]", "[12,2,7,8,6,11,22]", 
       "[8,2,5,8,4,15,13]", "[9,2,7,4,6,11,7]", 
       "[25,34]")
data = data.frame(week, visits)

The above data frame records daily visit in a string column, and each row represents a week.上面的数据框在一个字符串列中记录了每天的访问,每一行代表一周。

I would like to convert the above dataset to a long table with daily visits.我想将上述数据集转换为一个每天访问的长表。 This is the expected output:这是预期的 output:

date日期 visits访问
2022-9-1 2022-9-1 1 1个
2022-9-2 2022-9-2 2 2个
2022-9-3 2022-9-3 6 6个
2022-9-3 2022-9-3 6 6个
.... ....
2022-9-30 2022-9-30 34 34

I have tried the following steps: (1) break down the string using strsplit and remove the first and last item (which is [ and ] );我尝试了以下步骤:(1)使用strsplit分解字符串并删除第一个和最后一个项目(即[] ); (2) store each day's visit in separate columns; (2) 将每天的访问存储在单独的列中; (3) use dplyr::mutate and dplyr::complete to fill the dates between weeks; (3) 使用dplyr::mutatedplyr::complete来填充周之间的日期; (4) then loop over each first day of the week to make each week's wide data to long data. (4) 然后循环每个星期的第一天,使每个星期的宽数据成为长数据。 All these add to 60ish lines of code, where I feel that there might be an easier solution.所有这些都增加了 60 多行代码,我觉得可能会有更简单的解决方案。

Thank you so much in advance!非常感谢您!

Perhaps this helps也许这有帮助

library(dplyr)
library(tidyr)
library(stringr)
data %>% 
  mutate(visits = str_remove_all(visits, "\\]|\\[")) %>% 
  separate_rows(visits) %>%
  group_by(week) %>% 
  mutate(date = seq(first(week), length.out = n(), 
     by = '1 day'), .before = 'week') %>%
  ungroup %>%
  select(-week)

-output -输出

# A tibble: 30 × 2
   date       visits
   <date>     <chr> 
 1 2022-09-01 1     
 2 2022-09-02 2     
 3 2022-09-03 6     
 4 2022-09-04 4     
 5 2022-09-05 6     
 6 2022-09-06 11    
 7 2022-09-07 19    
 8 2022-09-08 12    
 9 2022-09-09 2     
10 2022-09-10 7     
# … with 20 more rows

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

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