0

I am new to R and doing some basic things. I wonder how can I find mean for only some rows. For example, how can I find mean weight for korgi only? (There are actually 100 rows instead of 8 and rows 1-50 are korgi) I tried korgiweight <-aggregate(mytabledata[1:50], list(mytabledata$Weight(kg), mean) But it says 'undefined columns selected' Thanks.

**Breed Weight(kg)** Korgi 10 korgi 11 korgi 10.5 korgi 12 husky 20 husky 24 husky 26 husky 25

  • 1
    Does this answer your question? [Calculate the mean by group](https://stackoverflow.com/questions/11562656/calculate-the-mean-by-group) – jogo May 04 '20 at 08:50

2 Answers2

2

There are many ways to calculate grouped means. The one you have been trying to do is via aggregate. It works like this:

aggregate(weight ~ species, data = df, mean)
  species weight
1   Korgi   18.8
2     XYZ   17.9

If you want to compute the mean only for a certain range of rows, you need to subset either the dataframe or both variables on that range:

aggregate(weight ~ species, data = df[1:10,], mean)
# or:
aggregate(weight[1:10] ~ species[1:10], data = df, mean)

  species[1:10] weight[1:10]
1         Korgi         18.8 

A related function is tapply, which applies a function, here mean, to a table:

tapply(df$weight, df$species, mean)
Korgi   XYZ 
 18.8  17.9 

Illustrative data:

set.seed(123)
df <- data.frame(species = c(rep("Korgi", 10), rep("XYZ", 10)),
                 weight = sample(10:25, 20, replace = T))
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
  • On SO it is customary to express gratitude by clicking the upward arrow in the upper left corner of the answer to indicate that the answer was useful and/or to click the tick in the corner to indicate that you accept the answer as the one that solved your problem best. – Chris Ruehlemann May 04 '20 at 11:26
0

You can do :

korgiweight <- mean(df$Weight.kg.[df$Breed == 'korgi'], na.rm = TRUE)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213