I want to transform a date range stored as two columns (start, end) + value to two new columns of just the date and value.
my data:
id end start value
1 4421 2014-01-01 2014-01-03 10
2 4421 2014-01-04 2014-01-04 500
3 4421 2014-01-05 2014-01-07 20
4 5560 2014-01-02 2014-01-03 100
5 5560 2014-01-04 2014-01-04 600
What I want:
Date id value
0 2014-01-01 4421 10
1 2014-01-02 4421 10
2 2014-01-03 4421 10
3 2014-01-04 4421 500
4 2014-01-05 4421 20
5 2014-01-06 4421 20
6 2014-01-07 4421 20
7 2014-01-01 5560 NA
8 2014-01-02 5560 100
9 2014-01-03 5560 100
10 2014-01-04 5560 600
I’m using dplyr, so something that I can use with mutate & pipes etc. would be useful.
Sample data:
id <- c(4421, 4421, 4421, 5560, 5560)
start <- c('2014-01-01','2014-01-04','2014-01-05','2014-01-02','2014-01-04')
end = c('2014-01-03','2014-01-04','2014-01-07','2014-01-03','2014-01-04')
value <- c(10,500,20,100,600)
my_data <- data.frame(id,start,end,value)
FYI there's a very similar question in python, but I'm using R.
Edit: formatting Edit 2: this is a duplicate, there's some great stuff in the original post.
Thanks @www I like that there’s pipes all the way. @Wen-Ben thanks for the Pandas tips, I may be using Pandas in future.