Home Ask Login Register

Developers Planet

Your answer is one click away!

stats123 February 2016

Manipulation of columns of data in R

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?

Answers


edouard February 2016

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 February 2016

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]

Post Status

Asked in February 2016
Viewed 2,295 times
Voted 6
Answered 2 times

Search




Leave an answer


Quote of the day: live life