I have next dataframe:
df <- data.frame(Date= as.Date(c("2020-08-04","2020-08-04","2020-08-06","2020-08-06","2020-08-07","2020-08-07")),
Period= c("Day","Night","Day","Night","Day","Night"),
State.1= c(1, 0.45,0.48,0.32,0.29,0.87),
State.2= c(0, 0.55,0.28,0.62,0.79,0.17))
df
Date Period State.1 State.2
1 2020-08-04 Day 1.00 0.00
2 2020-08-04 Night 0.45 0.55
3 2020-08-06 Day 0.48 0.28
4 2020-08-06 Night 0.32 0.62
5 2020-08-07 Day 0.29 0.79
6 2020-08-07 Night 0.87 0.17
In which I indicate per date
and Period
some values for State.1
and State.2
. For illustration purposes, I would like to re-arrange my dataframe incorporating Period
within both State.1
and State.2
. I would expect to get next:
df2
Date State.1_day State.1_night State.2_day State.2_night
1 2020-08-04 1.00 0.45 0.00 0.55
2 2020-08-05 NA NA NA NA
3 2020-08-06 0.48 0.32 0.28 0.62
4 2020-08-07 0.29 0.87 0.79 0.17
How could I do it? I tried melt()
but I couldn't get what I wanted.
Thanks in advance.