I am trying to average every n-th object of a specific column in a dataframe using the following code. I understand that using the for-loop is computationally inefficient. This is why I would like to ask whether there is a more efficient way to create the average of every n-th row? My data looks a little bit like this.
set.seed(6218)
n <- 8760
s1 <- sample(30000:70000, n)
s2 <- sample(0:10000, n)
inDf <- cbind(s1, s2)
EDIT:
I call h_average like this: h_average(inDf, 24, 1, 1) This would mean that I average every first point of "every" 24 point subset. So the points 1, 25, 49, 73,... Also I only do this for the first column.
Thanks in advance, BenR
#' h_average
#'
#' Computing the average of every first, second, third, ... hour of the day/week
#'
#' @param data merged data
#' @param tstep hour-step representing the number of hours for a day/week
#' @param h hour, which should be averaged. Should be between 1 - 24/1 - 168.
#' @param x column number
#' @return mean average of the specific hour
h_average <- function(data, tstep, h, x) {
sum_1 <- 0
sum_2 <- 0
mean <- 0
for (i in seq(h, nrow(data), tstep)){
if(data[i,x]){
sum_1 <- sum_1 + 1
sum_2 <- sum_2 + data[i,x]
}
}
mean <- sum_2/sum_1
return(mean)
}
If the question is how to reproduce
h_average
but without the loop then1) colMeans Try this:
This also works if
x
is a vector of column numbers, e.g.x = 1:2
.1a) This variation works too:
2) aggregate Another possibility is this:
which has the advantage that both
x
andh
may be vectors, e.g.To get all
h
then useh <- 1:tstep
or just omit[h, ]
.Note:
InDf
as defined in the question is a matrix and not a data frame as its name seems to suggest.Update Some improvements in (1) and added (1a) and (2).
Just use a combination of
rowMeans
and subsetting. So something like:Alternatively, you could use
apply