Iwn Aggelidou
Iwn Aggelidou

Reputation: 33

Calculate the mean,min and max of every 15 rows in data frame

I have a data frame with 77 columns and 1080 row df I want to reduce my data so, i want for every column: to find the min, max and mean of every 15 rows. For example, for the 1st column i want for every 15 rows to find min, max , mean, for the second column the same and so on and on for all the 77 columns, and put all these into a dataframe. How can i do it? Thank you

Upvotes: 1

Views: 924

Answers (2)

rg255
rg255

Reputation: 4169

Using base R and apply functions:

# dummy data from akrun
df1 <- data.frame(matrix(rnorm(77*1080, 0, 1), ncol = 77))

# Vectors with means, mins, and maxes for each row
means <- apply(df1, 1, mean)
mins  <- apply(df1, 1, min)
maxs  <- apply(df1, 1, max)

# Make to a data frame
df2 <- data.frame(means[seq(1, 1080, 15)], mins[seq(1, 1080, 15)], maxs[seq(1, 1080, 15)])

Or without creating additional objects (the vectors means, mins, and maxs) just go directly into a data frame:

df2 <- data.frame("Mean" = apply(df1, 1, mean)[seq(1, 1080, 15)], "Min" = apply(df1, 1, min)[seq(1, 1080, 15)], "Max" = apply(df1, 1, max)[seq(1, 1080, 15)])

And to reduce computation time, reduce the dataframe first:

df1 <- df1[seq(1, 1080, 15),]
df2 <- data.frame("Mean" = apply(df1, 1, mean), "Min" = apply(df1, 1, min), "Max" = apply(df1, 1, max))

Upvotes: 1

akrun
akrun

Reputation: 887991

We can create a grouping variable with gl and then do apply the functions with summarise_all

library(dplyr)
df %>% 
   group_by(group = as.integer(gl(n(), 15, n()))) %>% 
   summarise_all(funs(min, max, mean))

data

set.seed(24)
df <- as.data.frame(matrix(sample(1:9, 35 * 10, replace = TRUE), nrow = 35))

Upvotes: 4

Related Questions