user6644063 user6644063 - 4 months ago 13
R Question

Calculate groups of rows mean

I need to calculate the mean of groups of rows in 10 different data.frames and instead of making a code for each data.frame, I want to do it in only one command.

The code (which works for one data.frame) is:

means <- aggregate(df1[, 9], list(df1$height), mean)


I am just trying to apply it to all my 10 data.frames at the same time. Assuming that my data.frames are named df1, df2, df3, ..., df10 and that all of them have a column
height
, I tried:

for (i in 1:10) {
aggregate(df[i][, 9], list(df[i]$height), mean)
}


But I got the following error:

Error in aggregate(df[i][, 9], list(df[i]$height), mean) :
object 'df' not found


Any hint?




Why I got -1 ?

Answer

With this code:

for (i in 1:10) {
    aggregate(df[i][, 9], list(df[i]$height), mean)
}

You are doing this:

aggregate(df[1][, 9], list(df[1]$height), mean)
aggregate(df[2][, 9], list(df[2]$height), mean)
...

Note that df[1] will obtain the first column of the dataframe "df". You never defined a dataframe "df", so the code returns an error.

Instead, you want to use the paste0 function, which will connect all of its arguments to a single string. For instance,i <- 2 and paste0("hello", i) returns hello2.

So, assuming that all your dataframes are structured similarly, you could use code like:

for (i in 1:10) {
    aggregate(paste0("df", i)[, 9], list(paste0("df", i)$height), mean)
}