Home > other >  Unable to view only the means when using aggregate()
Unable to view only the means when using aggregate()

Time:12-25

I'm trying to get the average weight and height by age (along with other variables) from my dataset using the aggregate function but can't get it to work.

The following code gives me everything from the min, quartiles, mean etc. but I only want the mean.

table1 <- aggregate(data[,c("height", "weight")], by=list(age=data$age), summary)
View(table1)

I've tried this instead but I just get a table of height and weight by all my ages filled with NA values.

table1 <- aggregate(data[,c("height", "weight")], by=list(age=data$age), mean)
View(table1)

Thank you for any help.

CodePudding user response:

Try passing na.rm=TRUE to the mean function:


aggregate(data[,c("height", "weight")], by=list(age=data$age),\(x) mean(x,na.rm=T))


 
  • Related