calculate SD and mean simultaneously in tapply

I write this code to calculate the mean of 2 groups saved in one column. how can I calculate their SD at the same time?

tapply(df$age, df$Gender, mean , na.rm = TRUE)
1 2
42.61189 41.21101

If you just want to run one line of code, it's a way:

tapply(df$age, df$Gender, function(x) c("mean"=mean(x, na.rm=T), "sd"=sd(x, na.rm=T)))

But I don't know what you'll gain in this way. Why don't you run separate tapply calls for mean and sd?

thank you.
I am just starting to learn R and I thought I should write in one line.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.