某些行的标准差

standardvariation for certain rows

我有一个很长的数据集,其中包含案例行中提到的不同类型的问题。

age <- ("18-30","31-45","60+","46-60", "31-45", "18-30", "60+", "46-60")
gender <- ("M","F","F","F","M","M","F","M")
case <- ("Q1","Q1","Q2","Q2","Q3","Q3","Q4","Q4")
height <- (0,200,310,0,0,175,270,150)

我想计算身高列每个问题的平均值、中位数和标准差。 Q1、Q2、Q3 和 Q4 有 4 个不同的表。我对 r 的了解真的很有限有人可以帮助我吗? 提前致谢

library(dplyr)
df <- tibble(
  age = c("18-30","31-45","60+","46-60", "31-45", "18-30", "60+", "46-60"),
gender = c("M","F","F","F","M","M","F","M"),
case = c("Q1","Q1","Q2","Q2","Q3","Q3","Q4","Q4"),
height = c(0,200,310,0,0,175,270,150)
)

df %>% 
  group_by(case) %>% 
  summarise(mean = mean(height), 
            median = median(height), 
            sd = sd(height))

如果您想要每个案例的单独数据框,您可以简单地 filter 回答您想要的问题,即第一个案例“Q1”

df  %>% 
      group_by(case) %>% 
      summarise(mean = mean(height), 
                median = median(height), 
                sd = sd(height)) %>%
      filter(case == "Q1")