簡體   English   中英

如何在R中制作描述性統計表

[英]How to make a table of descriptive statistics in R

這是我現在擁有的數據,它是一個包含來自不同教育水平(F1)的學生的數據集以及關於他們學習成績數據的幾個變量 我想用 R 來制作一個像這樣的描述性統計表Result ,它有每個變量組的均值和標准差(按教育水平)。

aggregate(. ~ F1, dt3, function(x) c(mean = mean(x), sd = sd(x)))

我用過這個功能,但結果和我想要的不一樣。

這是我的數據的一個小樣本。

structure(list(F1 = c("Elementary school", "High_school", "High_school", "Elementary school", "Junior_high_school", "High_school", "Kindergarten", "Kindergarten"), X1 = c(0, 0, 0, 0, 0, 0, 0, 0), X2 = c(1, 1, 0, 0, 0, 0, 1, 1), X3 = c(1, 1, 1, 0, 0, 0, 0, 1), X4 = c(1, 1, 1, 1, 0, 1, 1, 1), X5 = c(4, 4, 4, 4, 1, 1, 4, 4), X6 = c(4, 4, 3, 4, 1, 2, 4, 4), X7 = c(4, 4, 3, 4, 3, 1, 4, 4), X8 = c(4, 4, 3, 4, 1, 1, 4, 4), Y1 = c(4, 4, 3, 4, 2, 3, 4, 4), Y2 = c(4, 3, 4, 3, 4, 3, 4, 4)), row.names = c(1L, 2L, 5L, 14L, 696L, 15L, 1348L, 1364L), class = "data.frame")

@NingyaoXu,請提供一個可重現的數據集示例

根據您的數據集的捕獲圖像和您的示例(我在您的示例中用education替換了F1 ),我建議您使用dplyrtidyr 你可以試試:

df %>% 
  pivot_longer(., -education, names_to = "Variable", values_to = "Value") %>%
  group_by(education, Variable) %>%
  summarise(Mean = mean(Value), Sd = sd(Value)) %>%
  pivot_wider(., names_from = "education", values_from = c(Mean, Sd))%>%
  select(., Variable, contains("Elementary school"), contains("High_school",ignore.case = F), contains("Junior_high_school",ignore.case = F),contains("Kindergarten"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM