Bit of a basic problem (I think), I have 2 columns with say, 1 with gender (1=girl,0=boy) and the other with age. How would I go about calculating the mean age of girls?
            Asked
            
        
        
            Active
            
        
            Viewed 53 times
        
    -1
            
            
        - 
                    1Something like: `tapply(age, gender, mean)` – DatamineR Feb 06 '16 at 20:08
- 
                    2if you're new, the "frequent" tab may be useful including [this question](http://stackoverflow.com/questions/3505701/r-grouping-functions-sapply-vs-lapply-vs-apply-vs-tapply-vs-by-vs-aggrega) – rawr Feb 06 '16 at 20:26
2 Answers
2
            
            
        aggregate is one option
weight <- c(12,34,56,78,33)
sex <- c('m','f','f','f','m')
df <- data.frame(weight,sex)
aggregate(df$weight, list(sex = df$sex), mean)
#  sex    x
#1   f 56.0
#2   m 22.5
 
    
    
        Brandon Bertelsen
        
- 43,807
- 34
- 160
- 255
 
    
    
        tagoma
        
- 3,896
- 4
- 38
- 57
0
            
            
        base
I like by when I'm working in the console too: 
weight <- c(12,34,56,78,33)
sex <- c('m','f','f','f','m')
df <- data.frame(weight,sex)
by(df,INDICES = df$sex, function(x) mean(df$weight))
dplyr
library(dplyr)
df %>% group_by(sex) %>% summarize(mean = mean(weight))
data.table
library(data.table)
dt <- setDT(df)
dt[,mean(weight),by = sex]
 
    
    
        Brandon Bertelsen
        
- 43,807
- 34
- 160
- 255
