简体   繁体   中英

How do I find standard deviation for gender specific sorted data set in R?

So, I am using the data.frame 'studentdata' and I have sorted the males from the females. I also created a new column called HoursSlept. Now I must find the standard deviation for males in the column HoursSlept and the same for Females.

Can someone help me??

Here is what I did but I don't know what sd it is giving me.

R控制台代码

If you want to do it your way you'd have to write:

sd(Males$HoursSlept, na.rm = T)
sd(Females$HoursSlept, na.rm = T)

Because Males and Females are data.frames and you have to pass one column from the data.frames to the function. A more elegant way would be not to split the data in two data.frames. Instead you could use dplyr's filter function.

library(dplyr)
studentdata %>%
    filter(Gender == "male") %>%
    summarise(sd = sd(HoursSlept, na.rm = T))

And the same for the females. Or as @MrGumble suggested both at once with group_by:

studentdata %>%
   group_by(Gender) %>%
   summarise(sd = sd(HoursSlept, na.rm = T))
library(Hmisc)
attach(studentdata)
summarize(HoursSlept,gender,sd)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM