I'm having problem with calculating the average for 2 variables in my data. I have temperature and speed collected as shown below. I want to minimize the data and have hourly
readings only (without minute). I want to take the average of the temperature
and the speed
for each id based on the time
and the day
.
ID temp Speed Day Hour Minute Latitude Longitude
1 3 20 1 11 10 38.9294865 -77.2479055
1 5 25 1 11 30 38.9294865 -77.2479055
1 5 30 1 12 12 38.9294865 -77.2479055
1 6 20 1 12 40 38.9294865 -77.2479055
2 1 40 2 11 05 38.9294771 -77.2478712
2 5 30 2 11 50 38.9294771 -77.2478712
2 2 20 2 12 30 38.9294771 -77.2478712
2 8 10 2 12 40 38.9294771 -77.2478712
My desired data looks like this:
ID temp Speed Day Hour Minute Latitude Longitude
1 4 22.5 1 11 00 38.9294865 -77.2479055
1 5.5 25 1 12 00 38.9294865 -77.2479055
2 3 30 2 11 00 38.9294771 -77.2478712
2 5 15 2 12 00 38.9294771 -77.2478712
I thought about creating a column with the hour and the minute like this :
Data$HM <- as.date(with(Data, paste(Hour, Minute ,sep=":")), "%H:%M")
And then based on the new column I was thinking to try this code:
AvrgData<- aggregate(Data[, 2:3], list(Data$HM), mean)
However my code is not correct. Any suggestions will be much appreciated! I went through these links but still not helping with the results I want.
How to average columns based on ID in R?
Merge three different columns into a date in R
Thank you