变换r中类别列的频次计数数据帧

  • 本文关键字:数数 数据帧 变换 r dplyr
  • 更新时间 :
  • 英文 :


我使用下面的代码创建了一个频率计数。

df %>% group_by(INCOME, HAPPY) %>% summarise(count=n())

输出:

INCOME HAPPY count
<int> <int> <int>
1      1     1     6
2      1     2    17
3      1     3    13
4      1     8     1
5      2     1     5
6      2     2    11
7      2     3    12
8      2     8     0
9      3     1     4
10      3     2    10
11      3     3     5
12      3     8     0

然而,我希望有以下频率格式:

1       2       3
1   6       5       4
2   17      11      10
3   13      12      5
8   1       0       0

使用xtabsfrombase R

xtabs(count ~  HAPPY + INCOME, df1)
INCOME
HAPPY  1  2  3
1  6  5  4
2 17 11 10
3 13 12  5
8  1  0  0

数据
df1 <- structure(list(INCOME = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L), HAPPY = c(1L, 2L, 3L, 8L, 1L, 2L, 3L, 8L, 1L, 2L, 
3L, 8L), count = c(6L, 17L, 13L, 1L, 5L, 11L, 12L, 0L, 4L, 10L, 
5L, 0L)), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12"))

代码后:df %>% group_by(INCOME, HAPPY) %>% summarise(count=n())

你可以使用下面的代码来完成你的任务:

library(dplyr)
library(tidyr)
library(tibble)
df %>% 
mutate(group_id = as.integer(gl(n(), 4, n()))) %>% 
pivot_wider(
HAPPY,
names_from = group_id,
values_from = count
) %>%
column_to_rownames("HAPPY")
1  2  3
1  6  5  4
2 17 11 10
3 13 12  5
8  1  0  0

数据:

structure(list(INCOME = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L), HAPPY = c(1L, 2L, 3L, 8L, 1L, 2L, 3L, 8L, 1L, 2L, 
3L, 8L), count = c(6L, 17L, 13L, 1L, 5L, 11L, 12L, 0L, 4L, 10L, 
5L, 0L)), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12"))

我认为这可以简化为-

library(dplyr)
library(tidyr)
df %>%
count(INCOME, HAPPY) %>%
pivot_wider(names_from = INCOME, values_from = n)

最新更新