My data includes time stamps of activities completed by athletes over different periods. Each period goes for a maximum of twenty minutes. The corresponding weather conditions are noted.
I want to call out the first occurrence of weather and when conditions change. My question is similar to this question except I want the first occurrence and when a change occurs.
My data is structured as follows:
df <- data.frame(Time=c("0:00:00","0:01:00","0:02:40","0:12:09",
"0:00:00", "0:02:07","0:07:19","0:15:16",
"0:00:00", "0:03:00","0:08:40","0:13:29",
"0:00:00", "0:02:10","0:08:47","0:17:55"),
Athlete = c('Paul', 'Paul', 'Paul', 'Paul',
'Paul', 'Paul', 'Paul','Paul',
'Joe', 'Joe', 'Joe', 'Joe',
'Joe', 'Joe', 'Joe', 'Joe'),
Period = c('P1', 'P1', 'P1', 'P1',
'P2', 'P2', 'P2', 'P2',
'P1', 'P1', 'P1', 'P1',
'P2', 'P2', 'P2', 'P2'),
Weather = c('Sunny', 'Sunny', 'Sunny', 'Cloudy',
'Rain', 'Cloudy', 'Rain', 'Rain',
'Rain', 'Sunny', 'Rain', 'Rain',
'Sunny', 'Sunny', 'Cloudy', 'Cloudy'))
- How do I call out the first occurrence and change in weather, according to each athlete and period?
- How do I have time in minutes.seconds? For example: 2.40
I have attempted the code below but this is not returning my anticipated output.
Test <- df[match(unique(df$Weather), df$Weather),]
My anticipated output would be:
Output <- data.frame(Time = c(0.0, 12.09,
0.0, 2.07, 7.19,
0.0, 3.00, 8.40,
0.0, 8.47),
Athlete = c('Paul', 'Paul',
'Paul', 'Paul', 'Paul',
'Joe', 'Joe', 'Joe',
'Joe', 'Joe'),
Period = c('P1', 'P1',
'P2', 'P2', 'P2',
'P1', 'P1', 'P1',
'P2', 'P2'),
Weather = c('Sunny', 'Cloudy',
'Rain', 'Cloudy', 'Rain',
'Rain', 'Sunny', 'Rain',
'Sunny', 'Cloudy'))
From this question, I understand the index of a change in factors within a column can be located, how do I arrange this code to have my desired output?
Thank you.