I'm new to R (having used MATLAB before) and have tried for a long time to search for a solution for this but I can't find one for this (seemingly) quite easy problem. Here is the problem;
In the first column I have my time values over a couple of days (simplified in this example) and in the second column I have the values over which I want to average. What I want to do is to take all the values that belong to the same time and average over that. I am doing this over rather large data sets so doing this automatically would help greatly.
Let's set-up:
time = rep( c("00:00", "00:10", "00:20", "00:30", "00:40", "00:50", "01:00", "01:10"), 5)
values = c(sample(1:100, 40))
data = cbind(time, values)
So now I have my matrix with time & values in it and I would like to group all the values that have (eg) "00:00" and calculate the mean of this. After some searching I found out the aggregate()
function could help nicely, so I did the following;
aggregate(as.numeric(data[,-1]), by = list(sort(data[,1])), mean)
which has output
Group.1 x
1 00:00 77.2
2 00:10 59.2
3 00:20 51.0
4 00:30 49.4
5 00:40 51.4
6 00:50 33.4
7 01:00 33.8
8 01:10 51.6
So it seems to work nicely but when I calculate it by hand the mean of the values are all different. (For instance; for 00:00: (56+3+91+71+8)/5 = 45.8 and NOT 77.2), can anyone tell me what I am doing wrong?