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, 0, 0, 0, 0, 0, 0.6, 3, 1.4, 0, 0, 0.2, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0.6, 0.2, 2.6, 3, 0.4, 0, 0, 0, 0, 0, 0, 0, 
0.2, 0, 0, 0, 0.2, 0, 0, 0, 0, 0.8, 0.2, 0.2, 0, 0, 0, 0, 0, 
0.2, 0, 0.2, 0, 0.4, 0.4, 0.8, 0.8, 1.2, 1, 0.6, 0.6, 0.4, 0.2, 
0.2, 0.4, 0.2, 0.2, 0.4, 0.2, 0.8, 0, 0, 0.2, 0, 0, 0.2, 0, 0.2, 
0, 0.2, 0, 0.2, 0.2, 0, 0, 0, 0)), .Names = c("X2005.hourly", "X2006.hourly", 
"X2007.hourly"), class = "data.frame", row.names = c(NA, -744L))

And the function I used before:

    myfun<- function(x) {
      y<-as.matrix(sapply(x, as.numeric)) 
#Aggregating hourly data to 6,12 and 24 hourly data      
h<-sapply(c(1, 6, 12, 24), function(hrs) colSums(matrix(y, ncol=length(x)/hrs)))
      hours<-sapply(list(h[[1]], h[[2]], h[[3]], h[[4]]), function(qq) list(qq))
      names(hours)<-c("hourly", "h.6", "h.12", "h.24")
#Calculating means of 1, 6, 12 and 24 hourly data      
means<-sapply(list(hours[[1]], hours[[2]], hours[[3]], hours[[4]]), function(ss) mean(ss, na.rm = T))
    #means of 1, 6, 12 and 24 hourly data 
      mean1<-means[[1]]
      mean6<-means[[2]]
      mean12<-means[[3]]
      mean24<-means[[4]]
      meanlist<-list(mean1, mean6, mean12, mean24)
      names(meanlist) <- c("mean1", "mean6", "mean12", "mean24")
      return(meanlist)
    }

What I did: I am applying myfun with lapply function and I am getting results separately for each year. And there is no trouble here.

results<-lapply(mydata, myfun)

What I want:

As you see, there is h.24 term inside of names(hours) in the myfun function . To calculate mean of 24 hourly data, function is using h.24 data aggregated from hourly data. But I want to use external 24 hourly daily data instead of h.24. In the data below, there are daily values separately for 2005,2006,2007

adddata<-structure(list(X2005.daily = c(0, 0, 3.4, 21.4, 1.8, 1.4, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 8.6, 1.4, 0, 0, 0, 0, 0.6, 7, 6.4, 21.8, 
3.2, 0, 0, 0.6, 11.2, 0), X2006.daily = c(0, 8.6, 11.2, 8.4, 
0, 0, 0, 0, 0, 7.8, 7.6, 1.2, 10.2, 7, 0, 0, 1.6, 7.6, 23.6, 
3.2, 0, 9, 31.4, 10, 2.2, 1.6, 0, 4.8, 10, 1.2, 4.6), X2007.daily = c(0.6, 
9.4, 4.2, 7.2, 15, 15, 0.4, 0, 8.6, 0, 0.2, 0.8, 0, 0.6, 0.6, 
1.4, 8.2, 0.2, 0, 0, 0, 0, 5, 1.6, 4, 5.2, 0, 0, 7, 4.2, 7.2)), .Names = c("X2005.daily", 
"X2006.daily", "X2007.daily"), class = "data.frame", row.names = c(NA, 
-31L))

Briefly: I want to add external daily data for each year, and then I want to calculate means of 1, 6, 12 hourly values and also means of added daily data for each year. So, the results of mean1, mean6, mean12 will be the same but mean24 will be the mean of added daily values.

Heikki
  • 2,214
  • 19
  • 34

1 Answers1

1

If I understood your question correctly, this should work:

results <- do.call(cbind,results) #convert your list to a data.frame for better overview
colnames(results) <- c("X2005","X2006","X2007")
results <- rbind(results,meanDaily=colMeans(adddata)) #rbind the colMeans of you adddata

results

          X2005        X2006        X2007
mean1     0.1193548    0.2336022    0.1430108   
mean6     0.716129     1.401613     0.8580645   
mean12    1.432258     2.803226     1.716129    
mean24    2.864516     5.606452     3.432258 
meanDaily 2.864516     5.574194     3.43871  

Additional answer to request in comment:

library(zoo)

myfun <- function(df_hourly,df_daily)
{

  DF <- sapply(df_hourly,function(col){c(mean(rollapply(col,width=1,sum,by=1,align="left")),
                                mean(rollapply(col,width=6,sum,by=6,align="left")),
                                mean(rollapply(col,width=12,sum,by=12,align="left")),
                                mean(rollapply(col,width=24,sum,by=24,align="left")))})
  DF <- rbind(DF,colMeans(adddata))

  colnames(DF) <- c("yr2005","yr2006","yr2007")
  rownames(DF) <- c("mean.1hr","mean.6hr","mean.12hr","mean.24hr","mean.daily")
  return(DF)
}


myfun(mydata,adddata)

              yr2005    yr2006    yr2007
mean.1hr   0.1193548 0.2336022 0.1430108
mean.6hr   0.7161290 1.4016129 0.8580645
mean.12hr  1.4322581 2.8032258 1.7161290
mean.24hr  2.8645161 5.6064516 3.4322581
mean.daily 2.8645161 5.5741935 3.4387097
Julian_Hn
  • 2,086
  • 1
  • 8
  • 18
  • Thanks for answer. But after finding `mean`, I need to find variance, autocovariance etc. So I guess, I need solution not only for mean, but also for another values. – Hüsamettin Tayşi Apr 17 '19 at 14:56
  • Then just use the appropiate function instead of `colMeans` – Julian_Hn Apr 17 '19 at 14:56
  • One more question, what if I want to use these codes in function? I mean for these codes, I need to find results and then add `meanDaily` to `results`. But I want to obtain it without finding result? – Hüsamettin Tayşi Apr 17 '19 at 15:13
  • I added a solution for your problem combined with a much easier computation of the rolling means using `zoo` – Julian_Hn Apr 17 '19 at 17:41
  • I am sorry for repeater questions. But how can I use the new code in `lapply` loop? For example, I will import almost 680 different `hourly` and `daily`data. Each data is consisting hourly and daily columns. – Hüsamettin Tayşi Apr 18 '19 at 09:31
  • well just lapply over your datasets. If you are not able to do this, please ask a new question to your specific problem and provide a reproducible example for this – Julian_Hn Apr 18 '19 at 09:32