gpt4 book ai didi

R DPLYR 按组计数值

转载 作者:行者123 更新时间:2023-12-02 01:29:36 25 4
gpt4 key购买 nike

HAVE = data.frame(  STUDENT =c( 1,1,2,2,2,3,3   ),
TEST =c( 'A','B','A','B','C','A','C' ))

WANT = data.frame(STUDENT=c(1,2,3),
A=c(1,1,1),
B=c(1,1,0),
C=c(0,1,1),
TOT=c(2,3,2),
TOT.NOT.A=c(1,2,1))

我有一个垂直数据,希望转换为水平数据,如上所示。我能做到

WANT = HAVE %>% group_by(STUDENT) %>% mutate(TOT = n_distinct (TEST)) 

要获取“TOT”,但我不知道如何获取“A”“B”“C”或“TOT.NOT.A”

最佳答案

我们可以使用 pivot_wider reshape 为“宽”格式并获取“TOT”所有列

library(dplyr)
library(tidyr)
HAVE %>%
pivot_wider(names_from = TEST, values_from = TEST,
values_fn = length, values_fill = 0) %>%
mutate(TOT = rowSums(across(-STUDENT), na.rm = TRUE),
TOT_NOT_A = rowSums(across(B:C), na.rm = TRUE))

-输出

# A tibble: 3 × 6
STUDENT A B C TOT TOT_NOT_A
<dbl> <int> <int> <int> <dbl> <dbl>
1 1 1 1 0 2 1
2 2 1 1 1 3 2
3 3 1 0 1 2 1

或者使用基础R

out <- addmargins(table(HAVE), 2)
cbind(out, TOT_NOT_A = rowSums(out[, c("B", "C")]))
A B C Sum TOT_NOT_A
1 1 1 0 2 1
2 1 1 1 3 2
3 1 0 1 2 1

关于R DPLYR 按组计数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73623938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com