I have dataframe with the following structure:
structure(list(`Monday_04:00` = c(0, 0, 0), `Monday_04:15` = c(0,
0, 0), `Monday_04:30` = c(0, 0, 0), `Monday_04:45` = c(0, 0,
0), `Monday_05:00` = c(0, 0, 0), `Monday_05:15` = c(0, 0, 0),
`Monday_05:30` = c(0, 0, 0), `Monday_05:45` = c(0, 0, 0),
`Monday_06:00` = c(0, 0, 0), `Monday_06:15` = c(0, 0, 0),
`Monday_06:30` = c(0, 0, 0), `Monday_06:45` = c(0, 0, 0),
`Monday_07:00` = c(0, 0, 0), `Monday_07:15` = c(0, 0, 0),
`Monday_07:30` = c(0, 0, 0), `Monday_07:45` = c(0, 0, 0),
`Monday_08:00` = c(2, 0, 0), `Monday_08:15` = c(2, 0, 0),
`Monday_08:30` = c(2, 0, 0), `Monday_08:45` = c(2, 0, 0),
`Monday_09:00` = c(2, 0, 0), `Monday_09:15` = c(2, 0, 0),
`Monday_09:30` = c(2, 0, 0), `Monday_09:45` = c(2, 0, 0),
`Monday_10:00` = c(2, 0, 0), `Monday_10:15` = c(2, 0, 0),
`Monday_10:30` = c(2, 0, 0), `Monday_10:45` = c(2, 0, 0),
`Monday_11:00` = c(2, 0, 0), `Monday_11:15` = c(2, 0, 0),
`Monday_11:30` = c(2, 0, 0), `Monday_11:45` = c(2, 0, 0),
`Monday_12:00` = c(0, 0, 0), `Monday_12:15` = c(0, 0, 0),
`Monday_12:30` = c(0, 0, 0), `Monday_12:45` = c(0, 0, 0),
`Monday_13:00` = c(2, 0, 0), `Monday_13:15` = c(2, 0, 0),
`Monday_13:30` = c(2, 0, 0), `Monday_13:45` = c(2, 0, 0),
`Monday_14:00` = c(2, 0, 0), `Monday_14:15` = c(2, 0, 0),
`Monday_14:30` = c(2, 0, 0), `Monday_14:45` = c(2, 0, 0),
`Monday_15:00` = c(2, 0, 0), `Monday_15:15` = c(2, 0, 0),
`Monday_15:30` = c(2, 0, 0), `Monday_15:45` = c(2, 0, 0),
`Monday_16:00` = c(2, 0, 0), `Monday_16:15` = c(2, 0, 0),
`Monday_16:30` = c(2, 0, 0), `Monday_16:45` = c(2, 0, 0),
`Monday_17:00` = c(2, 0, 0), `Monday_17:15` = c(2, 0, 0),
`Monday_17:30` = c(2, 0, 0), `Monday_17:45` = c(2, 0, 0),
`Monday_18:00` = c(2, 0, 0), `Monday_18:15` = c(2, 0, 0),
`Monday_18:30` = c(0, 0, 0), `Monday_18:45` = c(0, 0, 0),
`Monday_19:00` = c(0, 0, 0), `Monday_19:15` = c(0, 0, 0),
`Monday_19:30` = c(0, 0, 0), `Monday_19:45` = c(0, 0, 0),
`Monday_20:00` = c(0, 0, 0), `Monday_20:15` = c(0, 0, 0),
`Monday_20:30` = c(0, 0, 0), `Monday_20:45` = c(0, 0, 0),
`Monday_21:00` = c(0, 0, 0), `Monday_21:15` = c(0, 0, 0),
`Monday_21:30` = c(0, 0, 0), `Monday_21:45` = c(0, 0, 0),
`Monday_22:00` = c(0, 0, 0), `Monday_22:15` = c(0, 0, 0),
`Monday_22:30` = c(0, 0, 0), `Monday_22:45` = c(0, 0, 0),
`Monday_23:00` = c(0, 0, 0), `Monday_23:15` = c(0, 0, 0),
`Monday_23:30` = c(0, 0, 0), `Monday_23:45` = c(0, 0, 0),
`Monday_00:00` = c(0, 0, 0), `Monday_00:15` = c(0, 0, 0),
`Monday_00:30` = c(0, 0, 0), `Monday_00:45` = c(0, 0, 0),
`Monday_01:00` = c(0, 0, 0), `Monday_01:15` = c(0, 0, 0),
`Monday_01:30` = c(0, 0, 0), `Monday_01:45` = c(0, 0, 0),
`Monday_02:00` = c(0, 0, 0), `Monday_02:15` = c(0, 0, 0),
`Monday_02:30` = c(0, 0, 0), `Monday_02:45` = c(0, 0, 0),
`Monday_03:00` = c(0, 0, 0), `Monday_03:15` = c(0, 0, 0),
`Monday_03:30` = c(0, 0, 0), `Monday_03:45` = c(0, 0, 0),
`Tuesday_04:00` = c(0, 0, 0), `Tuesday_04:15` = c(0, 0, 0
), `Tuesday_04:30` = c(0, 0, 0), `Tuesday_04:45` = c(0, 0,
0), `Tuesday_05:00` = c(0, 0, 0), `Tuesday_05:15` = c(0,
0, 0), `Tuesday_05:30` = c(0, 0, 0), `Tuesday_05:45` = c(0,
0, 0), `Tuesday_06:00` = c(0, 0, 0), `Tuesday_06:15` = c(0,
0, 0), `Tuesday_06:30` = c(0, 0, 0), `Tuesday_06:45` = c(0,
0, 0), `Tuesday_07:00` = c(0, 0, 0), `Tuesday_07:15` = c(0,
0, 0), `Tuesday_07:30` = c(0, 0, 0), `Tuesday_07:45` = c(0,
0, 0), `Tuesday_08:00` = c(2, 0, 0), `Tuesday_08:15` = c(2,
0, 0), `Tuesday_08:30` = c(2, 0, 2), `Tuesday_08:45` = c(2,
0, 2), `Tuesday_09:00` = c(2, 0, 2), `Tuesday_09:15` = c(2,
0, 2), `Tuesday_09:30` = c(2, 0, 2), `Tuesday_09:45` = c(2,
0, 2), `Tuesday_10:00` = c(2, 0, 2), `Tuesday_10:15` = c(2,
0, 2), `Tuesday_10:30` = c(2, 0, 2), `Tuesday_10:45` = c(2,
0, 2), `Tuesday_11:00` = c(2, 0, 2), `Tuesday_11:15` = c(2,
0, 2), `Tuesday_11:30` = c(2, 0, 2), `Tuesday_11:45` = c(2,
0, 2), `Tuesday_12:00` = c(0, 0, 2), `Tuesday_12:15` = c(0,
0, 2), `Tuesday_12:30` = c(0, 0, 2), `Tuesday_12:45` = c(0,
0, 2), `Tuesday_13:00` = c(2, 0, 2), `Tuesday_13:15` = c(2,
0, 2), `Tuesday_13:30` = c(2, 0, 2), `Tuesday_13:45` = c(2,
0, 2), `Tuesday_14:00` = c(2, 0, 2), `Tuesday_14:15` = c(2,
0, 2), `Tuesday_14:30` = c(2, 0, 2), `Tuesday_14:45` = c(2,
0, 2), `Tuesday_15:00` = c(2, 0, 2), `Tuesday_15:15` = c(2,
0, 2), `Tuesday_15:30` = c(2, 0, 2), `Tuesday_15:45` = c(2,
0, 0), `Tuesday_16:00` = c(2, 0, 0), `Tuesday_16:15` = c(2,
0, 0), `Tuesday_16:30` = c(2, 0, 0), `Tuesday_16:45` = c(2,
0, 0), `Tuesday_17:00` = c(2, 0, 0), `Tuesday_17:15` = c(2,
0, 0), `Tuesday_17:30` = c(2, 0, 0), `Tuesday_17:45` = c(2,
0, 0), `Tuesday_18:00` = c(2, 0, 0), `Tuesday_18:15` = c(2,
0, 0), `Tuesday_18:30` = c(0, 0, 0), `Tuesday_18:45` = c(0,
0, 0), `Tuesday_19:00` = c(0, 0, 0), `Tuesday_19:15` = c(0,
0, 0), `Tuesday_19:30` = c(0, 0, 0), `Tuesday_19:45` = c(0,
0, 0), `Tuesday_20:00` = c(0, 0, 0), `Tuesday_20:15` = c(0,
0, 0), `Tuesday_20:30` = c(0, 0, 0), `Tuesday_20:45` = c(0,
0, 0), `Tuesday_21:00` = c(0, 0, 0), `Tuesday_21:15` = c(0,
0, 0), `Tuesday_21:30` = c(0, 0, 0), `Tuesday_21:45` = c(0,
0, 0), `Tuesday_22:00` = c(0, 0, 0), `Tuesday_22:15` = c(0,
0, 0), `Tuesday_22:30` = c(0, 0, 0), `Tuesday_22:45` = c(0,
0, 0), `Tuesday_23:00` = c(0, 0, 0), `Tuesday_23:15` = c(0,
0, 0), `Tuesday_23:30` = c(0, 0, 0), `Tuesday_23:45` = c(0,
0, 0), `Tuesday_00:00` = c(0, 0, 0), `Tuesday_00:15` = c(0,
0, 0), `Tuesday_00:30` = c(0, 0, 0), `Tuesday_00:45` = c(0,
0, 0), `Tuesday_01:00` = c(0, 0, 0), `Tuesday_01:15` = c(0,
0, 0), `Tuesday_01:30` = c(0, 0, 0), `Tuesday_01:45` = c(0,
0, 0), `Tuesday_02:00` = c(0, 0, 0), `Tuesday_02:15` = c(0,
0, 0), `Tuesday_02:30` = c(0, 0, 0), `Tuesday_02:45` = c(0,
0, 0), `Tuesday_03:00` = c(0, 0, 0), `Tuesday_03:15` = c(0,
0, 0), `Tuesday_03:30` = c(0, 0, 0), `Tuesday_03:45` = c(0,
0, 0), `Wednesday_04:00` = c(0, 0, 0), `Wednesday_04:15` = c(0,
0, 0), `Wednesday_04:30` = c(0, 0, 0), `Wednesday_04:45` = c(0,
0, 0), `Wednesday_05:00` = c(0, 0, 0), `Wednesday_05:15` = c(0,
0, 0), `Wednesday_05:30` = c(0, 0, 0), `Wednesday_05:45` = c(0,
0, 0), `Wednesday_06:00` = c(0, 0, 0), `Wednesday_06:15` = c(0,
0, 0), `Wednesday_06:30` = c(0, 0, 0), `Wednesday_06:45` = c(0,
0, 0), `Wednesday_07:00` = c(0, 0, 0), `Wednesday_07:15` = c(0,
0, 0), `Wednesday_07:30` = c(0, 0, 0), `Wednesday_07:45` = c(0,
0, 0), `Wednesday_08:00` = c(0, 0, 2), `Wednesday_08:15` = c(0,
0, 2), `Wednesday_08:30` = c(0, 0, 2), `Wednesday_08:45` = c(0,
0, 2), `Wednesday_09:00` = c(0, 0, 2), `Wednesday_09:15` = c(0,
0, 2), `Wednesday_09:30` = c(0, 0, 2), `Wednesday_09:45` = c(0,
0, 2), `Wednesday_10:00` = c(0, 0, 2), `Wednesday_10:15` = c(0,
0, 2), `Wednesday_10:30` = c(0, 0, 2), `Wednesday_10:45` = c(0,
0, 2), `Wednesday_11:00` = c(0, 0, 2), `Wednesday_11:15` = c(0,
0, 2), `Wednesday_11:30` = c(0, 0, 2), `Wednesday_11:45` = c(0,
0, 2), `Wednesday_12:00` = c(0, 0, 2), `Wednesday_12:15` = c(0,
0, 2), `Wednesday_12:30` = c(0, 0, 2), `Wednesday_12:45` = c(0,
0, 2), `Wednesday_13:00` = c(0, 0, 2), `Wednesday_13:15` = c(0,
0, 2), `Wednesday_13:30` = c(0, 0, 2), `Wednesday_13:45` = c(0,
0, 2), `Wednesday_14:00` = c(0, 0, 2), `Wednesday_14:15` = c(0,
0, 2), `Wednesday_14:30` = c(0, 0, 2), `Wednesday_14:45` = c(0,
0, 2), `Wednesday_15:00` = c(0, 0, 2), `Wednesday_15:15` = c(0,
0, 2), `Wednesday_15:30` = c(0, 0, 2), `Wednesday_15:45` = c(0,
0, 2), `Wednesday_16:00` = c(0, 0, 2), `Wednesday_16:15` = c(0,
0, 2), `Wednesday_16:30` = c(0, 0, 2), `Wednesday_16:45` = c(0,
0, 2), `Wednesday_17:00` = c(0, 0, 2), `Wednesday_17:15` = c(0,
0, 2), `Wednesday_17:30` = c(0, 0, 0), `Wednesday_17:45` = c(0,
0, 0), `Wednesday_18:00` = c(0, 0, 0), `Wednesday_18:15` = c(0,
0, 0), `Wednesday_18:30` = c(0, 0, 0), `Wednesday_18:45` = c(0,
0, 0), `Wednesday_19:00` = c(0, 0, 0), `Wednesday_19:15` = c(0,
0, 0), `Wednesday_19:30` = c(0, 0, 0), `Wednesday_19:45` = c(0,
0, 0), `Wednesday_20:00` = c(0, 0, 0), `Wednesday_20:15` = c(0,
0, 0), `Wednesday_20:30` = c(0, 0, 0), `Wednesday_20:45` = c(0,
0, 0), `Wednesday_21:00` = c(0, 0, 0), `Wednesday_21:15` = c(0,
0, 0), `Wednesday_21:30` = c(0, 0, 0), `Wednesday_21:45` = c(0,
0, 0), `Wednesday_22:00` = c(0, 0, 0), `Wednesday_22:15` = c(0,
0, 0), `Wednesday_22:30` = c(0, 0, 0), `Wednesday_22:45` = c(0,
0, 0), `Wednesday_23:00` = c(0, 0, 0), `Wednesday_23:15` = c(0,
0, 0), `Wednesday_23:30` = c(0, 0, 0), `Wednesday_23:45` = c(0,
0, 0), `Wednesday_00:00` = c(0, 0, 0), `Wednesday_00:15` = c(0,
0, 0), `Wednesday_00:30` = c(0, 0, 0), `Wednesday_00:45` = c(0,
0, 0), `Wednesday_01:00` = c(0, 0, 0), `Wednesday_01:15` = c(0,
0, 0), `Wednesday_01:30` = c(0, 0, 0), `Wednesday_01:45` = c(0,
0, 0), `Wednesday_02:00` = c(0, 0, 0), `Wednesday_02:15` = c(0,
0, 0), `Wednesday_02:30` = c(0, 0, 0), `Wednesday_02:45` = c(0,
0, 0), `Wednesday_03:00` = c(0, 0, 0), `Wednesday_03:15` = c(0,
0, 0), `Wednesday_03:30` = c(0, 0, 0), `Wednesday_03:45` = c(0,
0, 0), `Thursday_04:00` = c(0, 0, 0), `Thursday_04:15` = c(0,
0, 0), `Thursday_04:30` = c(0, 0, 0), `Thursday_04:45` = c(0,
0, 0), `Thursday_05:00` = c(0, 0, 0), `Thursday_05:15` = c(0,
0, 0), `Thursday_05:30` = c(0, 0, 0), `Thursday_05:45` = c(0,
0, 0), `Thursday_06:00` = c(0, 0, 0), `Thursday_06:15` = c(0,
0, 0), `Thursday_06:30` = c(0, 0, 0), `Thursday_06:45` = c(0,
0, 0), `Thursday_07:00` = c(0, 0, 0), `Thursday_07:15` = c(0,
0, 0), `Thursday_07:30` = c(0, 0, 0), `Thursday_07:45` = c(0,
0, 0), `Thursday_08:00` = c(0, 0, 0), `Thursday_08:15` = c(0,
0, 0), `Thursday_08:30` = c(0, 0, 2), `Thursday_08:45` = c(0,
0, 2), `Thursday_09:00` = c(0, 0, 2), `Thursday_09:15` = c(0,
0, 2), `Thursday_09:30` = c(0, 0, 2), `Thursday_09:45` = c(0,
0, 2), `Thursday_10:00` = c(0, 0, 2), `Thursday_10:15` = c(0,
0, 2), `Thursday_10:30` = c(0, 0, 2), `Thursday_10:45` = c(0,
0, 2), `Thursday_11:00` = c(0, 0, 2), `Thursday_11:15` = c(0,
0, 2), `Thursday_11:30` = c(0, 0, 2), `Thursday_11:45` = c(0,
0, 2), `Thursday_12:00` = c(0, 0, 2), `Thursday_12:15` = c(0,
0, 2), `Thursday_12:30` = c(0, 0, 2), `Thursday_12:45` = c(0,
0, 2), `Thursday_13:00` = c(0, 0, 2), `Thursday_13:15` = c(0,
0, 2), `Thursday_13:30` = c(0, 0, 2), `Thursday_13:45` = c(0,
0, 2), `Thursday_14:00` = c(0, 0, 2), `Thursday_14:15` = c(0,
0, 2), `Thursday_14:30` = c(0, 0, 2), `Thursday_14:45` = c(0,
0, 2), `Thursday_15:00` = c(0, 0, 2), `Thursday_15:15` = c(0,
0, 2), `Thursday_15:30` = c(0, 0, 2), `Thursday_15:45` = c(0,
0, 2), `Thursday_16:00` = c(0, 0, 2), `Thursday_16:15` = c(0,
0, 2), `Thursday_16:30` = c(0, 0, 2), `Thursday_16:45` = c(0,
0, 2), `Thursday_17:00` = c(0, 0, 0), `Thursday_17:15` = c(0,
0, 0), `Thursday_17:30` = c(0, 0, 0), `Thursday_17:45` = c(0,
0, 0), `Thursday_18:00` = c(0, 0, 0), `Thursday_18:15` = c(0,
0, 0), `Thursday_18:30` = c(0, 0, 0), `Thursday_18:45` = c(0,
0, 0), `Thursday_19:00` = c(0, 0, 0), `Thursday_19:15` = c(0,
0, 0), `Thursday_19:30` = c(0, 0, 0), `Thursday_19:45` = c(0,
0, 0), `Thursday_20:00` = c(0, 0, 0), `Thursday_20:15` = c(0,
0, 0), `Thursday_20:30` = c(0, 0, 0), `Thursday_20:45` = c(0,
0, 0), `Thursday_21:00` = c(0, 0, 0), `Thursday_21:15` = c(0,
0, 0), `Thursday_21:30` = c(0, 0, 0), `Thursday_21:45` = c(0,
0, 0), `Thursday_22:00` = c(0, 0, 0), `Thursday_22:15` = c(0,
0, 0), `Thursday_22:30` = c(0, 0, 0), `Thursday_22:45` = c(0,
0, 0), `Thursday_23:00` = c(0, 0, 0), `Thursday_23:15` = c(0,
0, 0), `Thursday_23:30` = c(0, 0, 0), `Thursday_23:45` = c(0,
0, 0), `Thursday_00:00` = c(0, 0, 0), `Thursday_00:15` = c(0,
0, 0), `Thursday_00:30` = c(0, 0, 0), `Thursday_00:45` = c(0,
0, 0), `Thursday_01:00` = c(0, 0, 0), `Thursday_01:15` = c(0,
0, 0), `Thursday_01:30` = c(0, 0, 0), `Thursday_01:45` = c(0,
0, 0), `Thursday_02:00` = c(0, 0, 0), `Thursday_02:15` = c(0,
0, 0), `Thursday_02:30` = c(0, 0, 0), `Thursday_02:45` = c(0,
0, 0), `Thursday_03:00` = c(0, 0, 0), `Thursday_03:15` = c(0,
0, 0), `Thursday_03:30` = c(0, 0, 0), `Thursday_03:45` = c(0,
0, 0), `Friday_04:00` = c(0, 0, 0), `Friday_04:15` = c(0,
0, 0), `Friday_04:30` = c(0, 0, 0), `Friday_04:45` = c(0,
0, 0), `Friday_05:00` = c(0, 0, 0), `Friday_05:15` = c(0,
0, 0), `Friday_05:30` = c(0, 0, 0), `Friday_05:45` = c(0,
0, 0), `Friday_06:00` = c(0, 0, 0), `Friday_06:15` = c(0,
0, 0), `Friday_06:30` = c(0, 0, 0), `Friday_06:45` = c(0,
0, 0), `Friday_07:00` = c(0, 0, 0), `Friday_07:15` = c(0,
0, 0), `Friday_07:30` = c(0, 0, 0), `Friday_07:45` = c(0,
0, 0), `Friday_08:00` = c(0, 0, 0), `Friday_08:15` = c(2,
0, 0), `Friday_08:30` = c(2, 0, 2), `Friday_08:45` = c(2,
0, 2), `Friday_09:00` = c(2, 0, 2), `Friday_09:15` = c(2,
0, 2), `Friday_09:30` = c(2, 0, 2), `Friday_09:45` = c(2,
0, 2), `Friday_10:00` = c(2, 0, 2), `Friday_10:15` = c(2,
0, 2), `Friday_10:30` = c(2, 0, 2), `Friday_10:45` = c(2,
0, 2), `Friday_11:00` = c(2, 0, 2), `Friday_11:15` = c(2,
0, 2), `Friday_11:30` = c(2, 0, 0), `Friday_11:45` = c(2,
0, 0), `Friday_12:00` = c(0, 0, 0), `Friday_12:15` = c(0,
0, 0), `Friday_12:30` = c(0, 0, 0), `Friday_12:45` = c(0,
0, 0), `Friday_13:00` = c(0, 0, 0), `Friday_13:15` = c(2,
0, 0), `Friday_13:30` = c(2, 0, 0), `Friday_13:45` = c(2,
0, 0), `Friday_14:00` = c(2, 0, 0), `Friday_14:15` = c(2,
0, 0), `Friday_14:30` = c(2, 2, 0), `Friday_14:45` = c(2,
2, 0), `Friday_15:00` = c(2, 2, 0), `Friday_15:15` = c(2,
2, 0), `Friday_15:30` = c(2, 2, 0), `Friday_15:45` = c(2,
2, 0), `Friday_16:00` = c(2, 2, 0), `Friday_16:15` = c(2,
2, 0), `Friday_16:30` = c(2, 2, 0), `Friday_16:45` = c(2,
2, 0), `Friday_17:00` = c(2, 2, 0), `Friday_17:15` = c(2,
2, 0), `Friday_17:30` = c(2, 2, 0), `Friday_17:45` = c(2,
2, 0), `Friday_18:00` = c(2, 2, 0), `Friday_18:15` = c(0,
2, 0), `Friday_18:30` = c(0, 2, 0), `Friday_18:45` = c(0,
2, 0), `Friday_19:00` = c(0, 2, 0), `Friday_19:15` = c(0,
2, 0), `Friday_19:30` = c(0, 0, 0), `Friday_19:45` = c(0,
0, 0), `Friday_20:00` = c(0, 0, 0), `Friday_20:15` = c(0,
0, 0), `Friday_20:30` = c(0, 0, 0), `Friday_20:45` = c(0,
0, 0), `Friday_21:00` = c(0, 0, 0), `Friday_21:15` = c(0,
0, 0), `Friday_21:30` = c(0, 0, 0), `Friday_21:45` = c(0,
0, 0), `Friday_22:00` = c(0, 0, 0), `Friday_22:15` = c(0,
0, 0), `Friday_22:30` = c(0, 0, 0), `Friday_22:45` = c(0,
0, 0), `Friday_23:00` = c(0, 0, 0), `Friday_23:15` = c(0,
0, 0), `Friday_23:30` = c(0, 0, 0), `Friday_23:45` = c(0,
0, 0), `Friday_00:00` = c(0, 0, 0), `Friday_00:15` = c(0,
0, 0), `Friday_00:30` = c(0, 0, 0), `Friday_00:45` = c(0,
0, 0), `Friday_01:00` = c(0, 0, 0), `Friday_01:15` = c(0,
0, 0), `Friday_01:30` = c(0, 0, 0), `Friday_01:45` = c(0,
0, 0), `Friday_02:00` = c(0, 0, 0), `Friday_02:15` = c(0,
0, 0), `Friday_02:30` = c(0, 0, 0), `Friday_02:45` = c(0,
0, 0), `Friday_03:00` = c(0, 0, 0), `Friday_03:15` = c(0,
0, 0), `Friday_03:30` = c(0, 0, 0), `Friday_03:45` = c(0,
0, 0), `Saturday_04:00` = c(0, 0, 0), `Saturday_04:15` = c(0,
0, 0), `Saturday_04:30` = c(0, 0, 0), `Saturday_04:45` = c(0,
0, 0), `Saturday_05:00` = c(0, 0, 0), `Saturday_05:15` = c(0,
0, 0), `Saturday_05:30` = c(0, 0, 0), `Saturday_05:45` = c(0,
0, 0), `Saturday_06:00` = c(0, 0, 0), `Saturday_06:15` = c(0,
0, 0), `Saturday_06:30` = c(0, 0, 0), `Saturday_06:45` = c(0,
0, 0), `Saturday_07:00` = c(0, 0, 0), `Saturday_07:15` = c(0,
0, 0), `Saturday_07:30` = c(0, 0, 0), `Saturday_07:45` = c(0,
0, 0), `Saturday_08:00` = c(0, 0, 0), `Saturday_08:15` = c(2,
0, 0), `Saturday_08:30` = c(2, 0, 0), `Saturday_08:45` = c(2,
0, 0), `Saturday_09:00` = c(2, 0, 0), `Saturday_09:15` = c(2,
0, 0), `Saturday_09:30` = c(2, 0, 0), `Saturday_09:45` = c(2,
0, 0), `Saturday_10:00` = c(2, 0, 0), `Saturday_10:15` = c(2,
0, 0), `Saturday_10:30` = c(2, 0, 0), `Saturday_10:45` = c(2,
0, 0), `Saturday_11:00` = c(2, 0, 0), `Saturday_11:15` = c(2,
0, 0), `Saturday_11:30` = c(2, 0, 0), `Saturday_11:45` = c(2,
0, 0), `Saturday_12:00` = c(0, 0, 0), `Saturday_12:15` = c(0,
0, 0), `Saturday_12:30` = c(0, 0, 0), `Saturday_12:45` = c(0,
0, 0), `Saturday_13:00` = c(2, 0, 0), `Saturday_13:15` = c(2,
0, 0), `Saturday_13:30` = c(2, 0, 0), `Saturday_13:45` = c(2,
0, 0), `Saturday_14:00` = c(2, 0, 0), `Saturday_14:15` = c(2,
0, 0), `Saturday_14:30` = c(2, 0, 0), `Saturday_14:45` = c(2,
0, 0), `Saturday_15:00` = c(2, 0, 0), `Saturday_15:15` = c(2,
0, 0), `Saturday_15:30` = c(2, 0, 0), `Saturday_15:45` = c(2,
0, 0), `Saturday_16:00` = c(2, 0, 0), `Saturday_16:15` = c(2,
0, 0), `Saturday_16:30` = c(2, 0, 0), `Saturday_16:45` = c(2,
0, 0), `Saturday_17:00` = c(2, 0, 0), `Saturday_17:15` = c(2,
0, 0), `Saturday_17:30` = c(2, 0, 0), `Saturday_17:45` = c(2,
0, 0), `Saturday_18:00` = c(2, 0, 0), `Saturday_18:15` = c(0,
0, 0), `Saturday_18:30` = c(0, 0, 0), `Saturday_18:45` = c(0,
0, 0), `Saturday_19:00` = c(0, 0, 0), `Saturday_19:15` = c(0,
0, 0), `Saturday_19:30` = c(0, 0, 0), `Saturday_19:45` = c(0,
0, 0), `Saturday_20:00` = c(0, 0, 0), `Saturday_20:15` = c(0,
0, 0), `Saturday_20:30` = c(0, 0, 0), `Saturday_20:45` = c(0,
0, 0), `Saturday_21:00` = c(0, 0, 0), `Saturday_21:15` = c(0,
0, 0), `Saturday_21:30` = c(0, 0, 0), `Saturday_21:45` = c(0,
0, 0), `Saturday_22:00` = c(0, 0, 0), `Saturday_22:15` = c(0,
0, 0), `Saturday_22:30` = c(0, 0, 0), `Saturday_22:45` = c(0,
0, 0), `Saturday_23:00` = c(0, 0, 0), `Saturday_23:15` = c(0,
0, 0), `Saturday_23:30` = c(0, 0, 0), `Saturday_23:45` = c(0,
0, 0), `Saturday_00:00` = c(0, 0, 0), `Saturday_00:15` = c(0,
0, 0), `Saturday_00:30` = c(0, 0, 0), `Saturday_00:45` = c(0,
0, 0), `Saturday_01:00` = c(0, 0, 0), `Saturday_01:15` = c(0,
0, 0), `Saturday_01:30` = c(0, 0, 0), `Saturday_01:45` = c(0,
0, 0), `Saturday_02:00` = c(0, 0, 0), `Saturday_02:15` = c(0,
0, 0), `Saturday_02:30` = c(0, 0, 0), `Saturday_02:45` = c(0,
0, 0), `Saturday_03:00` = c(0, 0, 0), `Saturday_03:15` = c(0,
0, 0), `Saturday_03:30` = c(0, 0, 0), `Saturday_03:45` = c(0,
0, 0), `Sunday_04:00` = c(0, 0, 0), `Sunday_04:15` = c(0,
0, 0), `Sunday_04:30` = c(0, 0, 0), `Sunday_04:45` = c(0,
0, 0), `Sunday_05:00` = c(0, 0, 0), `Sunday_05:15` = c(0,
0, 0), `Sunday_05:30` = c(0, 0, 0), `Sunday_05:45` = c(0,
0, 0), `Sunday_06:00` = c(0, 0, 0), `Sunday_06:15` = c(0,
0, 0), `Sunday_06:30` = c(0, 0, 0), `Sunday_06:45` = c(0,
0, 0), `Sunday_07:00` = c(0, 0, 0), `Sunday_07:15` = c(0,
0, 0), `Sunday_07:30` = c(0, 0, 0), `Sunday_07:45` = c(0,
0, 0), `Sunday_08:00` = c(2, 0, 0), `Sunday_08:15` = c(2,
0, 0), `Sunday_08:30` = c(2, 0, 0), `Sunday_08:45` = c(2,
0, 0), `Sunday_09:00` = c(2, 0, 0), `Sunday_09:15` = c(2,
0, 0), `Sunday_09:30` = c(2, 0, 0), `Sunday_09:45` = c(2,
0, 0), `Sunday_10:00` = c(2, 0, 0), `Sunday_10:15` = c(2,
0, 0), `Sunday_10:30` = c(2, 0, 0), `Sunday_10:45` = c(2,
0, 0), `Sunday_11:00` = c(2, 0, 0), `Sunday_11:15` = c(2,
0, 0), `Sunday_11:30` = c(2, 0, 0), `Sunday_11:45` = c(2,
0, 0), `Sunday_12:00` = c(2, 0, 0), `Sunday_12:15` = c(2,
0, 0), `Sunday_12:30` = c(2, 0, 0), `Sunday_12:45` = c(2,
0, 0), `Sunday_13:00` = c(0, 0, 0), `Sunday_13:15` = c(0,
0, 0), `Sunday_13:30` = c(0, 0, 0), `Sunday_13:45` = c(0,
0, 0), `Sunday_14:00` = c(2, 0, 0), `Sunday_14:15` = c(2,
0, 0), `Sunday_14:30` = c(2, 0, 0), `Sunday_14:45` = c(2,
0, 0), `Sunday_15:00` = c(2, 0, 0), `Sunday_15:15` = c(2,
0, 0), `Sunday_15:30` = c(2, 0, 0), `Sunday_15:45` = c(2,
0, 0), `Sunday_16:00` = c(2, 0, 0), `Sunday_16:15` = c(2,
0, 0), `Sunday_16:30` = c(2, 0, 0), `Sunday_16:45` = c(2,
0, 0), `Sunday_17:00` = c(2, 0, 0), `Sunday_17:15` = c(2,
0, 0), `Sunday_17:30` = c(0, 0, 0), `Sunday_17:45` = c(0,
0, 0), `Sunday_18:00` = c(0, 0, 0), `Sunday_18:15` = c(0,
0, 0), `Sunday_18:30` = c(0, 0, 0), `Sunday_18:45` = c(0,
0, 0), `Sunday_19:00` = c(0, 0, 0), `Sunday_19:15` = c(0,
0, 0), `Sunday_19:30` = c(0, 0, 0), `Sunday_19:45` = c(0,
0, 0), `Sunday_20:00` = c(0, 0, 0), `Sunday_20:15` = c(0,
0, 0), `Sunday_20:30` = c(0, 0, 0), `Sunday_20:45` = c(0,
0, 0), `Sunday_21:00` = c(0, 0, 0), `Sunday_21:15` = c(0,
0, 0), `Sunday_21:30` = c(0, 0, 0), `Sunday_21:45` = c(0,
0, 0), `Sunday_22:00` = c(0, 0, 0), `Sunday_22:15` = c(0,
0, 0), `Sunday_22:30` = c(0, 0, 0), `Sunday_22:45` = c(0,
0, 0), `Sunday_23:00` = c(0, 0, 0), `Sunday_23:15` = c(0,
0, 0), `Sunday_23:30` = c(0, 0, 0), `Sunday_23:45` = c(0,
0, 0), `Sunday_00:00` = c(0, 0, 0), `Sunday_00:15` = c(0,
0, 0), `Sunday_00:30` = c(0, 0, 0), `Sunday_00:45` = c(0,
0, 0), `Sunday_01:00` = c(0, 0, 0), `Sunday_01:15` = c(0,
0, 0), `Sunday_01:30` = c(0, 0, 0), `Sunday_01:45` = c(0,
0, 0), `Sunday_02:00` = c(0, 0, 0), `Sunday_02:15` = c(0,
0, 0), `Sunday_02:30` = c(0, 0, 0), `Sunday_02:45` = c(0,
0, 0), `Sunday_03:00` = c(0, 0, 0), `Sunday_03:15` = c(0,
0, 0), `Sunday_03:30` = c(0, 0, 0), `Sunday_03:45` = c(0,
0, 0), index = 1:3), row.names = c(NA, 3L), class = "data.frame")
>
The first row (rn)contains numbers from 2 to 3523. The other 672 columns are time steps that record people works chedules: 0 - not worked and 2 - worked
I am trying to calculate: a.) daily workhours per id (sum of 95 columns/day).
b.) weekly workhours per id
Any suggestion is welcomed
I would use tidyr and dplyr to go from wide to long format and then group_by and summarize.
Try this:
library(tidyverse)
df %>%
gather(day_time, worked, -index) %>%
separate(day_time, c("day", "time"), "_") %>%
group_by(index, day) %>%
summarize(tot_work = sum(worked)/2*15/60)
#> # A tibble: 21 x 3
#> # Groups: index [?]
#> index day tot_work
#> <int> <chr> <dbl>
#> 1 1 Friday 8.75
#> 2 1 Monday 9.5
#> 3 1 Saturday 9
#> 4 1 Sunday 8.5
#> 5 1 Thursday 0
#> 6 1 Tuesday 9.5
#> 7 1 Wednesday 0
#> 8 2 Friday 5
#> 9 2 Monday 0
#> 10 2 Saturday 0
#> # … with 11 more rows
df %>%
gather(day_time, worked, -index) %>%
separate(day_time, c("day", "time"), "_") %>%
group_by(index) %>%
summarize(tot_work = sum(worked)/2 * 15 / 60) # divided by 2 * 15 min and divided by 60 min to have everything in hours
#> # A tibble: 3 x 2
#> index tot_work
#> <int> <dbl>
#> 1 1 45.2
#> 2 2 5
#> 3 3 28.2
If you want everything in minutes do not divide by 60.
Consider reshaping your data from the current wide format to long format. This is the ideal structure for most data analytics including aggregation and a storage efficient format as columns necessitate more meta data (i.e., tracking types) than rows.
# RESHAPE WIDE TO LONG
rdf <- reshape(df, idvar="index", varying=list(head(names(df),-1)), v.names="hours",
times = head(names(df),-1), timevar = "day_time",
new.row.names = 1:1E5, direction = "long")
# SPLIT DAY AND TIME
rdf[c("day", "time")] <- t(sapply(strsplit(rdf$day_time, split="_"), "["))
rdf$day_time <- NULL
head(rdf, 10)
# index hours day time
# 1 1 0 Monday 04:00
# 2 2 0 Monday 04:00
# 3 3 0 Monday 04:00
# 4 1 0 Monday 04:15
# 5 2 0 Monday 04:15
# 6 3 0 Monday 04:15
# 7 1 0 Monday 04:30
# 8 2 0 Monday 04:30
# 9 3 0 Monday 04:30
# 10 1 0 Monday 04:45
# SUMMARIZE HOURS BY INDEX
agg_df <- aggregate(hours ~ index, rdf, sum)
agg_df
# index hours
# 1 1 362
# 2 2 40
# 3 3 226
By weekday:
rdf$day <- factor(rdf$day, levels=c("Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"))
aggregate(hours ~ index + day, rdf, sum)
# day index hours
# 1 Monday 1 76
# 2 Tuesday 1 76
# 3 Wednesday 1 0
# 4 Thursday 1 0
# 5 Friday 1 70
# 6 Saturday 1 72
# 7 Sunday 1 68
# 8 Monday 2 0
# 9 Tuesday 2 0
# 10 Wednesday 2 0
# 11 Thursday 2 0
# 12 Friday 2 40
# 13 Saturday 2 0
# 14 Sunday 2 0
# 15 Monday 3 0
# 16 Tuesday 3 58
# 17 Wednesday 3 76
# 18 Thursday 3 68
# 19 Friday 3 24
# 20 Saturday 3 0
# 21 Sunday 3 0
I have a list (lst3, subset below) and would like to do some calculations on it, e.g.:
lst4 <-lapply(lst3, function(x) aggregate(x[,5:ncol(x)], x[c(4)], FUN = mean)) #column means
lst5<-lapply(lst4,function(x) apply(x[,-c(1)],1,mean)) # get row mean
However, I am unable to get row mean without ignoring "Site".
I would like my final list to look like this:
lst5<-
[[1]]
Site x
G116 1.864233
[[2]]
Site x
GG16 2.064567
The essence is that the final list should have the above structure so that I can write my data to working directory using:
lapply(lst5,function(x)write.table(x,file=paste(getwd(),"summer",paste0(unique(x$Site),".csv"),
sep="/"),row.names=FALSE,quote=FALSE)) ### create a folder called "summer" and write files to directory###
Thanks,
AZ.
list(structure(list(Year = c(2005L, 2005L, 2005L), Month = c(8L,
8L, 8L), Day = 29:31, Site = structure(c(1L, 1L, 1L), .Label = "G116", class = "factor"),
Sim001 = c(8.4, 17.72, 6.03), Sim002 = c(0.27, 0, 0), Sim003 = c(2.83,
0.14, 0.1), Sim004 = c(0, 0, 0), Sim005 = c(0, 0.77, 0.28
), Sim006 = c(0, 0, 0), Sim007 = c(0, 0, 0), Sim008 = c(10.94,
4.77, 0), Sim009 = c(0, 0, 0), Sim010 = c(3.43, 2.74, 0.65
), Sim011 = c(0.36, 0, 2.75), Sim012 = c(26.91, 0, 2.16),
Sim013 = c(0.88, 1.33, 0.87), Sim014 = c(0, 0.86, 9.42),
Sim015 = c(0, 0.17, 1.15), Sim016 = c(0, 0, 0), Sim017 = c(0.13,
0, 0), Sim018 = c(0, 0, 6.72), Sim019 = c(8.45, 12.99, 23.72
), Sim020 = c(1.76, 0, 0), Sim021 = c(0, 0, 2.34), Sim022 = c(0,
0, 0), Sim023 = c(1.2, 0, 0.26), Sim024 = c(0.85, 0, 0),
Sim025 = c(0, 0, 0), Sim026 = c(2.05, 0.76, 5.03), Sim027 = c(0.78,
0, 0), Sim028 = c(1.2, 0, 0), Sim029 = c(22, 0.19, 0), Sim030 = c(0.12,
0, 0), Sim031 = c(3.1, 13.67, 0), Sim032 = c(0, 0, 17.88),
Sim033 = c(0, 0, 0), Sim034 = c(1.11, 0, 0), Sim035 = c(1.17,
1.41, 23.35), Sim036 = c(0, 0.48, 1.71), Sim037 = c(1.51,
11.1, 7.98), Sim038 = c(0, 0, 0), Sim039 = c(0, 0, 5.46),
Sim040 = c(5.21, 0, 0), Sim041 = c(0.1, 0.11, 0), Sim042 = c(0,
0.15, 5.23), Sim043 = c(0, 0, 0), Sim044 = c(0, 0.1, 0),
Sim045 = c(0, 0, 0), Sim046 = c(0, 0, 0), Sim047 = c(0, 0,
0.11), Sim048 = c(0, 0, 0), Sim049 = c(0, 0, 4.05), Sim050 = c(0,
0, 0), Sim051 = c(0, 0.12, 0), Sim052 = c(0.24, 2.58, 0),
Sim053 = c(3.63, 0, 0.17), Sim054 = c(10.94, 2.69, 0), Sim055 = c(0,
0, 0), Sim056 = c(0.24, 0.44, 8.27), Sim057 = c(0, 0, 0),
Sim058 = c(0, 0, 3.75), Sim059 = c(0.19, 11.06, 0), Sim060 = c(0,
0, 1.65), Sim061 = c(0, 4.95, 0), Sim062 = c(0.15, 0, 4.73
), Sim063 = c(2.99, 0.12, 1.28), Sim064 = c(0, 0, 0), Sim065 = c(0,
0, 0), Sim066 = c(0, 0, 0), Sim067 = c(0.11, 0.62, 0.56),
Sim068 = c(2.84, 0, 0), Sim069 = c(0, 0, 0), Sim070 = c(17.91,
0.11, 4.78), Sim071 = c(0, 0, 1.68), Sim072 = c(0, 0, 1.38
), Sim073 = c(1.68, 0, 0), Sim074 = c(0.53, 0, 2.87), Sim075 = c(0,
0, 0), Sim076 = c(2.58, 0.27, 0.11), Sim077 = c(0, 0, 0),
Sim078 = c(9.07, 3.13, 8.62), Sim079 = c(0.98, 0, 2.38),
Sim080 = c(3.4, 0, 0), Sim081 = c(0, 0, 4.57), Sim082 = c(1.87,
2.86, 0), Sim083 = c(21.76, 2.24, 0), Sim084 = c(0.45, 4.03,
0.39), Sim085 = c(0, 0, 0), Sim086 = c(0, 0, 0), Sim087 = c(0,
0, 17.12), Sim088 = c(5.05, 0, 0), Sim089 = c(0, 0, 1.4),
Sim090 = c(0.1, 0, 0), Sim091 = c(1.96, 0, 1.38), Sim092 = c(0,
0, 0), Sim093 = c(0, 0, 0), Sim094 = c(0, 0, 1.81), Sim095 = c(2.72,
7.16, 1.7), Sim096 = c(6.37, 0, 0), Sim097 = c(0, 1.12, 25.7
), Sim098 = c(0, 0, 0), Sim099 = c(0, 0, 0), Sim100 = c(6.77,
10.87, 2.6)), .Names = c("Year", "Month", "Day", "Site",
"Sim001", "Sim002", "Sim003", "Sim004", "Sim005", "Sim006", "Sim007",
"Sim008", "Sim009", "Sim010", "Sim011", "Sim012", "Sim013", "Sim014",
"Sim015", "Sim016", "Sim017", "Sim018", "Sim019", "Sim020", "Sim021",
"Sim022", "Sim023", "Sim024", "Sim025", "Sim026", "Sim027", "Sim028",
"Sim029", "Sim030", "Sim031", "Sim032", "Sim033", "Sim034", "Sim035",
"Sim036", "Sim037", "Sim038", "Sim039", "Sim040", "Sim041", "Sim042",
"Sim043", "Sim044", "Sim045", "Sim046", "Sim047", "Sim048", "Sim049",
"Sim050", "Sim051", "Sim052", "Sim053", "Sim054", "Sim055", "Sim056",
"Sim057", "Sim058", "Sim059", "Sim060", "Sim061", "Sim062", "Sim063",
"Sim064", "Sim065", "Sim066", "Sim067", "Sim068", "Sim069", "Sim070",
"Sim071", "Sim072", "Sim073", "Sim074", "Sim075", "Sim076", "Sim077",
"Sim078", "Sim079", "Sim080", "Sim081", "Sim082", "Sim083", "Sim084",
"Sim085", "Sim086", "Sim087", "Sim088", "Sim089", "Sim090", "Sim091",
"Sim092", "Sim093", "Sim094", "Sim095", "Sim096", "Sim097", "Sim098",
"Sim099", "Sim100"), row.names = 15947:15949, class = "data.frame"),
structure(list(Year = c(2005L, 2005L, 2005L), Month = c(8L,
8L, 8L), Day = 29:31, Site = structure(c(1L, 1L, 1L), .Label = "GG16", class = "factor"),
Sim001 = c(18.36, 0.33, 0.14), Sim002 = c(0, 10.92, 0
), Sim003 = c(0, 0, 0), Sim004 = c(0, 0, 1.7), Sim005 = c(0,
0, 0), Sim006 = c(0.91, 4.24, 0), Sim007 = c(0, 0, 0.22
), Sim008 = c(0.63, 2.9, 2.24), Sim009 = c(0, 0, 0),
Sim010 = c(0, 0, 6.91), Sim011 = c(0, 3.28, 10.18), Sim012 = c(8.39,
14.58, 45.62), Sim013 = c(2.87, 0.53, 0.11), Sim014 = c(9.15,
21.1, 0.66), Sim015 = c(0, 1.75, 2.2), Sim016 = c(0,
7.86, 0), Sim017 = c(0, 0, 0), Sim018 = c(0, 0, 0), Sim019 = c(0,
0, 0), Sim020 = c(0.39, 0, 0), Sim021 = c(0.13, 0, 1.05
), Sim022 = c(0, 0, 10.91), Sim023 = c(0.23, 0, 0), Sim024 = c(0.12,
0.83, 5.35), Sim025 = c(0, 0, 0), Sim026 = c(7.75, 0,
4.82), Sim027 = c(20.04, 0, 0), Sim028 = c(12.41, 0,
5.3), Sim029 = c(0, 0, 0), Sim030 = c(0, 0, 0), Sim031 = c(0,
8.06, 0), Sim032 = c(0, 0, 0), Sim033 = c(0, 0, 0), Sim034 = c(0.1,
0, 3.34), Sim035 = c(0, 4.34, 3.53), Sim036 = c(2.89,
0.27, 0), Sim037 = c(0, 0, 0), Sim038 = c(0, 0, 0), Sim039 = c(0,
0.11, 0), Sim040 = c(9.83, 1.55, 9.09), Sim041 = c(3.6,
0, 0), Sim042 = c(0, 0, 1.37), Sim043 = c(0, 0, 0), Sim044 = c(0,
0, 0), Sim045 = c(0, 0, 0), Sim046 = c(0, 0, 0), Sim047 = c(0,
20.52, 0.65), Sim048 = c(1.77, 0.67, 0), Sim049 = c(0,
0, 0), Sim050 = c(0, 0, 0), Sim051 = c(0, 4.9, 0), Sim052 = c(0.71,
11.34, 0), Sim053 = c(3.46, 2.59, 1.5), Sim054 = c(0,
23.63, 0), Sim055 = c(0, 16.48, 4.99), Sim056 = c(0,
0, 0), Sim057 = c(0, 0, 0), Sim058 = c(0, 0, 0), Sim059 = c(0,
0, 0), Sim060 = c(16.87, 0, 0), Sim061 = c(0, 3.43, 0
), Sim062 = c(0.45, 0, 0), Sim063 = c(0, 11.14, 7.22),
Sim064 = c(0, 0, 0), Sim065 = c(0, 0, 0), Sim066 = c(0,
16.08, 1.87), Sim067 = c(0, 0, 0), Sim068 = c(5.16, 0.88,
0.1), Sim069 = c(0, 0, 3.91), Sim070 = c(0, 0, 0), Sim071 = c(0.17,
0, 5.22), Sim072 = c(0, 0, 6.95), Sim073 = c(0, 0, 0),
Sim074 = c(0.14, 0, 0), Sim075 = c(0, 0, 0), Sim076 = c(0,
9.62, 0), Sim077 = c(0, 0, 0), Sim078 = c(1.65, 0, 0),
Sim079 = c(0.23, 8.41, 0.28), Sim080 = c(0.78, 0, 0),
Sim081 = c(0, 0, 0), Sim082 = c(0.11, 2.75, 0), Sim083 = c(0.26,
7.34, 5.92), Sim084 = c(0, 0, 4.27), Sim085 = c(0, 0,
0), Sim086 = c(0, 0, 0.1), Sim087 = c(27.18, 0.72, 28.29
), Sim088 = c(0, 0, 4.2), Sim089 = c(0, 9.37, 6.59),
Sim090 = c(0.21, 2.57, 0), Sim091 = c(0.45, 0, 0), Sim092 = c(0,
4.97, 0), Sim093 = c(1.43, 0, 0), Sim094 = c(0, 0, 2.15
), Sim095 = c(6, 0, 1.63), Sim096 = c(7.21, 0, 0), Sim097 = c(0,
0.39, 1.92), Sim098 = c(0, 0, 0), Sim099 = c(4.38, 0,
0), Sim100 = c(0, 0, 0)), .Names = c("Year", "Month",
"Day", "Site", "Sim001", "Sim002", "Sim003", "Sim004", "Sim005",
"Sim006", "Sim007", "Sim008", "Sim009", "Sim010", "Sim011",
"Sim012", "Sim013", "Sim014", "Sim015", "Sim016", "Sim017",
"Sim018", "Sim019", "Sim020", "Sim021", "Sim022", "Sim023",
"Sim024", "Sim025", "Sim026", "Sim027", "Sim028", "Sim029",
"Sim030", "Sim031", "Sim032", "Sim033", "Sim034", "Sim035",
"Sim036", "Sim037", "Sim038", "Sim039", "Sim040", "Sim041",
"Sim042", "Sim043", "Sim044", "Sim045", "Sim046", "Sim047",
"Sim048", "Sim049", "Sim050", "Sim051", "Sim052", "Sim053",
"Sim054", "Sim055", "Sim056", "Sim057", "Sim058", "Sim059",
"Sim060", "Sim061", "Sim062", "Sim063", "Sim064", "Sim065",
"Sim066", "Sim067", "Sim068", "Sim069", "Sim070", "Sim071",
"Sim072", "Sim073", "Sim074", "Sim075", "Sim076", "Sim077",
"Sim078", "Sim079", "Sim080", "Sim081", "Sim082", "Sim083",
"Sim084", "Sim085", "Sim086", "Sim087", "Sim088", "Sim089",
"Sim090", "Sim091", "Sim092", "Sim093", "Sim094", "Sim095",
"Sim096", "Sim097", "Sim098", "Sim099", "Sim100"), row.names = 15947:15949, class = "data.frame"))
You can go from lst3 directly to lst5 without the intermediate aggregate step:
lapply(lst3, function(df){
data.frame(Site = df$Site[1], x = mean(unlist(df[-c(1:4)])))
})
#[[1]]
# Site x
#1 G116 1.864233
#
#[[2]]
# Site x
#1 GG16 2.064567
Since you're calculating the mean of all columns except the first 4 columns and over all the rows of the other columns, it's quite easy to unlist the data, creating a single vector, and then using standard mean on it. Also, by skipping the lst4 step, this most likely be noticeably faster.
Or, as commented by Richard, a variation could be:
lapply(lst3, function(df){
data.frame(Site = df$Site[1], x = mean(colMeans(df[-c(1:4)])))
})
Benchmark:
library(microbenchmark)
microbenchmark(
f1 = {lapply(lst3, function(df){
data.frame(Site = df$Site[1], x = mean(unlist(df[-c(1:4)])))
})},
f2 = {lapply(lst3, function(df){
data.frame(Site = df$Site[1], x = mean(colMeans(df[-c(1:4)])))
})},
unit = "relative"
)
Unit: relative
expr min lq median uq max neval
f1 1.00000 1.000000 1.000000 1.000000 1.000000 100
f2 2.91545 2.937272 2.927799 2.894704 3.486007 100
Here's another option for your consideration:
library(reshape2)
x <- melt(lst3)
aggregate(value ~ Site, x[grepl("^Sim.*", x$variable),], FUN = mean)
# Site value
#1 G116 1.864233
#2 GG16 2.064567
Or the same concept but using dplyr:
library(dplyr)
filter(x, grepl("^Sim.*", variable)) %>% group_by(Site) %>% summarise(x = mean(value))
#Source: local data frame [2 x 2]
#
# Site x
#1 G116 1.864233
#2 GG16 2.064567
Of course, this could also be done using data.table, for example like this (there are probably several even slightly more efficient ways to do this in data.table):
library(data.table)
setDT(x)[grepl("^Sim.*", variable), list(x = mean(value)), by = Site]
# Site x
#1: G116 1.864233
#2: GG16 2.064567