Economist_Ayahuasca
Economist_Ayahuasca

Reputation: 1642

building date variable by including start and end date in r

I have a data frame that looks like this:

id date start_date end_date
1  3    2          9
1  5    2          9
2  8    5          10
2  9    5          10

I would like to include start_date and end_date into the date column:

id date 
1  2
1  3    
1  5  
1  9
2  5  
2  8    
2  9 
2  10 

Upvotes: 0

Views: 54

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388817

Bring the data in long format and keep only unique rows for each id.

library(dplyr)
library(tidyr)

df %>%
  pivot_longer(cols = -id) %>%
  arrange(id, value) %>%
  distinct(id, value)

#     id value
#  <int> <int>
#1     1     2
#2     1     3
#3     1     5
#4     1     9
#5     2     5
#6     2     8
#7     2     9
#8     2    10

Upvotes: 2

Related Questions