I have a data frame for every second. From the data frame with 1 second interval, I managed to aggregate the data into 1-minute interval using the following code:
agg_cont <- df %>% group_by(Date, Hour, Minute, Status, Mean) %>% count(name = 'Occurrence')
Now I have a data frame as shown below,
Date | Hour | Minute | Status | Mean | Occurrence |
---|---|---|---|---|---|
12/01/2022 | 00 | 00 | a | 20 | 60 |
12/02/2022 | 00 | 01 | b | 32 | 60 |
12/01/2022 | 00 | 02 | a | 21 | 60 |
12/02/2022 | 00 | 03 | a | 12 | 60 |
12/01/2022 | 00 | 04 | a | 23 | 20 |
12/01/2022 | 00 | 04 | b | 43 | 40 |
12/01/2022 | 00 | 05 | a | 33 | 60 |
Please note that the column 'Occurrence' denotes the number of seconds the status occurred in the specific minute. For the minute '04', if the occurrence for status 'a' and 'b' is 20 and 40, respectively, then status 'a' occurred for 20 sec in the particular minute.
With the above dataframe, I would like to have only one row for each minute and create new columns for each 'status' and the mean value that occurred in that particular minute.
Desired output:
Date | Hour | Minute | a | b | Mean 'a' | Mean'b' |
---|---|---|---|---|---|---|
12/01/2022 | 00 | 00 | 60 | 0 | 20 | NA |
12/02/2022 | 00 | 01 | 0 | 60 | 32 | NA |
12/01/2022 | 00 | 02 | 60 | 0 | 21 | NA |
12/01/2022 | 00 | 03 | 0 | 60 | 12 | NA |
12/01/2022 | 00 | 04 | 20 | 40 | 23 | 43 |
12/02/2022 | 00 | 05 | 60 | 0 | 33 | NA |
I am trying to use the dcast function to get the desired output.
Thanks