I have data.frame in R, where one column is a list of dates (many of which are duplicates), while the other column is the temperature recorded on that date. These columns look like this (but these are several thousand rows and several other unnecessary columns):
Date | Temp
1/2/13 34.4
1/2/13 36.4
1/2/13 34.3
1/4/13 45.6
1/4/13 33.5
1/5/13 45.2
I need to find a way to get the daily average temperature. So ideally, I could tell R about going through the data.frame cycle and for each date that matches, give me the average value for the temperature that day. I was googling, and I know that loops in R are possible, but I can not plunge into this concept, given that I know little about R-code.
I know that I can pull out one column and average it (i.e. mean(data.frame[[2]])), but I completely lost the way I say R, so that it matches the single value located in the first column.
Also, how can I generate an average for every seven calendar days (no matter how many records exist in one day)? So, the seven-day moving average , i.e. If my date range starts from 1/1/13, I get the average for all the rates between 1/1/13 and 1/7/13, and then between 1/8/13 and 1/15/13, etc. ....
Any help helping me understand the R loops is greatly appreciated. Thank!
EDIT
dput(head(my.dataframe)) , : "", " ", :
structure(list(RECID = 579:584, SITEID = c(101L, 101L, 101L,
101L, 101L, 101L), MONTH = c(6L, 6L, 6L, 6L, 6L, 6L), DAY = c(7L,
7L, 7L, 7L, 7L, 7L), DATE = structure(c(34L, 34L, 34L, 34L, 34L,
34L), .Label = c("10/1/2013", "10/10/2013", "10/11/2013", "10/12/2013",
"10/2/2013", "10/3/2013", "10/4/2013", "10/5/2013", "10/6/2013",
"10/7/2013", "10/8/2013", "10/9/2013", "6/10/2013", "6/11/2013","9/9/2013"), class = "factor"), TIMESTAMP = structure(784:789, .Label = c("10/1/2013 0:00",
"10/1/2013 1:00", "10/1/2013 10:00", "10/1/2013 11:00", "10/1/2013 12:00",
"10/1/2013 13:00", "10/1/2013 14:00", "10/1/2013 15:00", "10/1/2013 16:00",
"10/1/2013 17:00", "10/1/2013 18:00", "10/1/2013 19:00", "10/1/2013 2:00"), class = "factor"), TEMP = c(23.376, 23.376, 23.833, 24.146,
24.219, 24.05), X.C = c(NA, NA, NA, NA, NA, NA)), .Names = c("RECID",
"SITEID", "MONTH", "DAY", "DATE", "TIMESTAMP", "TEMP", "X.C"), row.names = c(NA,
6L), class = "data.frame")