I have a dataframe of which I put one variable into a vector.
From this vector, I would like to calculate for every 5 values mean
, min
and max
value.
I have managed to calculate the means in this way:
means <- colMeans(matrix(df$values, nrow=5))
I know I can calculate the min and max like this:
max <- max(df$values[1:5])
min <- min(df$values[1:5])
How do I repeat this for every five values?
Edit:
Aditionally, how can I get statistic and p-value from a 1-sample t-test for each n-row?
For those who love
dplyr
and want to preserve the structure of the data, you can use theRcppRoll
packageNow finding the max
1) tapply Below
g
is a grouping variable consisting of fives ones, fives twos and so on.range
provides the minimum and maximum resulting in a list output fromtapply
and thensimplify2array
reduces that to an array. Omit thesimlify2array
if you want a list output.out[1, ]
would be the minima andout[2, ]
would be the maxima.giving:
2) aggregate This would also work:
giving this data.frame where the first column is
g
and the second column is the transpose of the matrix in (1). Hereag[[2]][, 1]
is the minima andag[[2]][, 2]
is the maxima. If you want to flattenag
trydo.call(data.frame, ag)
ordo.call(cbind, ag)
depending on whether you want a 3 column data frame or matrix.Certainly an atypical way to do it, and maybe not the most efficient, but you could try
zoo::rollapply
. This gives you more info than you need, but you can then filter down to only what you want:You can use
sapply
andsplit
for this:If you want the outputs in a matrix you can use what @lmo proposed in the comments:
Update
How to get statistic and p-value from a sample t-test for each n-row: This would be a bit harder to implement. Look below;
p_value_list
andstatistic_list
arep.value
andstatistic
for each 5 rows.