Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Spread values based on start-date/end-date

I have a dataframe which looks something like:

  start_date   end_date name value
1 2020-01-01 2020-01-06    x     2
2 2020-01-05 2020-01-07    y     4

I’d like to spread the dates by day, so that the name variables become columns taking their respective value, as so:

        date  x  y
1 2020-01-01  2 NA
2 2020-01-02  2 NA
3 2020-01-03  2 NA
4 2020-01-04  2 NA
5 2020-01-05  2  4
6 2020-01-06  2  4
7 2020-01-07 NA  4

I would be interested in a base R solution, but also solutions using dplyr and/or lubridate

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Convert the ‘_date’ columns to Date class (if it is not already in Date class), then create the ‘date’ column by getting the seq between the ‘start_date’, ‘end_date’ using map2 (or can also do after rowwise), unnest the list column and reshape into ‘wide’ format with pivot_wider

library(dplyr)
library(lubridate)
library(purrr)
library(tidyr)
df1 %>% 
  mutate(across(ends_with('_date'), ymd)) %>% 
  transmute(date = map2(start_date, end_date, seq, by = "1 day"), 
       name, value) %>% 
  unnest(date) %>%
  pivot_wider(names_from = name, values_from = value)

-output

# A tibble: 7 × 3
  date           x     y
  <date>     <int> <int>
1 2020-01-01     2    NA
2 2020-01-02     2    NA
3 2020-01-03     2    NA
4 2020-01-04     2    NA
5 2020-01-05     2     4
6 2020-01-06     2     4
7 2020-01-07    NA     4

data

df1 <- structure(list(start_date = c("2020-01-01", "2020-01-05"), 
end_date = c("2020-01-06", 
"2020-01-07"), name = c("x", "y"), value = c(2L, 4L)), 
class = "data.frame", row.names = c("1", 
"2"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading