Getting mean of a numeric variable according to the categorical variable in R


I was trying to calculate the mean of Variable Age for all the females in my dataset but I am getting error NaN. Kindly help me out with this
mean(Age[Gender == “Female”])
[1] NaN


Hi @sanchi_singh1

Please write reproducible code and try to provide sample data frame to remove ambiguity.

here is my try

  1. assuming “df” as dataframe

  2. DataManipulation using “dplyr”

     if(!require(dplyr)) {

#below code will give you mean age of male as well as female
df %>% group_by(Age) %>% summarize(Average=mean(Age)) %>% ungroup() %>% arrange(-Average) %>% data.frame


You have many options to do this:

#---------------- With data.table -------------
DT <- data.table(
Gender = ifelse( sample(c(0,1),100, replace = TRUE) ==0, “Female”, “Male”),
Age = sample(c(1:100), 100, replace = TRUE )

DT[ Gender == “Female”, .(AgeMean = mean(Age))]

#---------------- with the standard function “by” -------------
DF <-
by( DF[, 2], DF[,“Gender”], mean)

#-------------- with sqldf package -----------------------------
sqldf(“select Gender, avg(Age) as avgAge from DF group by Gender”)

Carlos Ortega - Spain


hi @sanchi_singh1

if you have age as separate variable in the dataset:
x<-#assign the data frame or read file


you’ll get age as returned value