簡體   English   中英

具有多個變量的頻率表,按分類變量分組

[英]Frequency table with multiple variables, grouped by a categorical variable

我想創建一個由分類變量(顏色)分組的多個變量(X1 - X4)的頻率表。 這是示例數據:

df <- data.frame(name = paste0("obj", 1:6),
                 X1 = c(0,1,1,1,0,1),
                 X2 = c(1,1,1,1,1,1),
                 X3 = c(0,1,1,0,0,0),
                 X4 = c(0,1,1,1,0,0),
                 color = c("red","red","blue","green","green","blue"),
                 other = c(5,3,1,8,4,3))

這就是 output 的理想外觀:

\begin{table}[]
\begin{tabular}{lllll}
Var & red & blue & green & total \\
X1  & 1   & 2    & 1     & 4     \\
X2  & 2   & 2    & 2     & 6     \\
X3  & 1   & 1    & 0     & 2     \\
X4  & 1   & 1    & 1     & 3    
\end{tabular}
\end{table}

非常感謝!

您可以獲取長格式的數據,並為每種color和列sum值,獲取寬格式的數據並添加Total列。

library(dplyr)
library(tidyr)

df %>%
  pivot_longer(cols = starts_with('X'), names_to = 'col') %>%
  group_by(col, color) %>%
  summarise(n = sum(value)) %>%
  pivot_wider(names_from = color, values_from = n) %>%
  ungroup %>%
  janitor::adorn_totals(where = 'col') 
  #Or use `rowSums`
  #mutate(Total = rowSums(.[-1]))

# col blue green red Total
#  X1    2     1   1     4
#  X2    2     2   2     6
#  X3    1     0   1     2
#  X4    1     1   1     3
 

暫無
暫無

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

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