gpt4 book ai didi

r - R 中的表操作 - dplyr/tidyverse 解决方案

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

我正在尝试将一个 df 转换为另一种格式。

开始 df:

df <- data.frame(id = c("1", "2", "3","4", "5", "6", "7", "8", "9", "10"),
criteria_A = c("present", "present", "absent", "absent", "absent", "present", "absent", "present", "absent", "present"),
criteria_B =c("absent", "absent", "present", "absent", "absent", "present", "absent", "absent", "present", "present"))

我想按存在/缺席对每个标准进行计数,并按如下方式重新制表:

df2 <- data.frame(criteria = c("criteria_A", "criteria_A", "criteria_B", "criteria_B"),
count = c("5", "5", "4", "6"),
status = c("present", "absent", "present", "absent"))

我考虑过按照标准以这种方式获取计数:

library(dplyr)
tmp1 <- df %>% group_by(criteria_A) %>% count() %>% mutate(criteria="criteria_A")
tmp1 <- tmp1 %>% rename(criteria_A=status)
tmp2 <- df %>% group_by(criteria_B) %>% count() %>% mutate(criteria="criteria_B")
tmp2 <- tmp2 %>% rename(criteria_B=status)

我想我可以垂直合并输出。当实际上我有数百个标准时,这不是一种有效或聪明的方法......

我确信有一个优雅的解决方案,但我不够聪明,无法弄清楚!

一如既往地提供任何帮助,我们将不胜感激。

最佳答案

在使用 pivot_longer 将数据转换为长格式后,您可以尝试使用 dplyr::tally

library(dplyr)

df %>%
pivot_longer(-id,
names_to = 'criteria',
values_to = 'status') %>%
group_by(criteria, status) %>%
tally

#----
criteria status n
<chr> <chr> <int>
1 criteria_A absent 5
2 criteria_A present 5
3 criteria_B absent 6
4 criteria_B present 4

关于r - R 中的表操作 - dplyr/tidyverse 解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65929024/

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