I have the following dataframe in R.
df <- data.frame(
"DateValue" = c("2016-07-01", "2016-07-02", "2016-07-03", "2016-07-04", "2016-07-05", "2016-07-06","2017-07-01", "2017-07-02", "2017-07-03", "2017-07-04", "2017-07-05", "2017-07-06", "2018-07-01", "2018-07-02", "2018-07-03", "2018-07-04", "2018-07-05", "2018-07-06"),
"Age1" = seq(1:18),
"Age2" = c(seq(14,36,2), rep(NA, 6)),
"Age3" = c(seq(45,50),rep(NA, 12))
)
DateValue Age1 Age2 Age3
# 1 2016-07-01 1 14 45
# 2 2016-07-02 2 16 46
# 3 2016-07-03 3 18 47
# 4 2016-07-04 4 20 48
# 5 2016-07-05 5 22 49
# 6 2016-07-06 6 24 50
# 7 2017-07-01 7 26 NA
# 8 2017-07-02 8 28 NA
# 9 2017-07-03 9 30 NA
# 10 2017-07-04 10 32 NA
# 11 2017-07-05 11 34 NA
# 12 2017-07-06 12 36 NA
# 13 2018-07-01 13 NA NA
# 14 2018-07-02 14 NA NA
# 15 2018-07-03 15 NA NA
# 16 2018-07-04 16 NA NA
# 17 2018-07-05 17 NA NA
# 18 2018-07-06 18 NA NA
I am trying to come up with a code that aligns the data from the "Age2" and "Age3" columns so that the dates line up. Below is the output I am looking for:
df <- data.frame(
"DateValue" = c("07-01", "07-02", "07-03", "07-04", "07-05", "07-06"),
"Age1" = seq(13:18),
"Age2" = seq(26,36,2),
"Age3" = seq(45,50)
)
# DateValue Age1 Age2 Age3
# 1 07-01 13 26 45
# 2 07-02 14 28 46
# 3 07-03 15 30 47
# 4 07-04 16 32 48
# 5 07-05 17 34 49
# 6 07-06 18 36 50
I am essentially keeping all the dates and values for my current year (2018) and matching them with the dates for the previous years. Note that I may have more dates in my previous year. But I need to drop all the rows that do not have any data for the current year. I reviewed the following thread on SO on rearranging the dataframe but the context is quite different than my situation. R Data Rearrange
I tried looking at the R reshape package but haven't had any luck. Any suggestions/ pointers would be appreciated.