gpt4 book ai didi

r - 如何按列计算每组的频率

转载 作者:行者123 更新时间:2023-12-02 16:11:02 26 4
gpt4 key购买 nike

我有一个名为 ballons 的数据集,其中包含两个集群:True 和 false。我正在搜索计算每个集群和每列的模态频率,所以我尝试了:

library(ggplot2)
library(tidyverse)

ballons=structure(list(YELLOW = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("PURPLE",
"YELLOW"), class = "factor"), SMALL = structure(c(2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L
), .Label = c("LARGE", "SMALL"), class = "factor"), STRETCH = structure(c(2L,
2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L,
1L, 1L), .Label = c("DIP", "STRETCH"), class = "factor"), ADULT = structure(c(1L,
2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L,
1L, 2L), .Label = c("ADULT", "CHILD"), class = "factor"), T = c(TRUE,
FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE,
FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE)), class = "data.frame", row.names = c(NA,
-19L))

r1=group_split(ballons %>%
group_by(T))

print(r1)


data.frame(do.call(rbind, lapply(r1,function(x) sapply(x,table))))


YELLOW SMALL STRETCH ADULT T
1 6, 6 6, 6 8, 4 4, 8 12
2 4, 3 4, 3 0, 7 7, 0 7

但是我正在搜索此表示(预期输出):

      YELLOW PURPLE SMALL LARGE ...
FALSE 6 6 6 6 ...
TRUE 4 3 4 3 etc

我也试过没有成功:

lapply(r1,function(x) sapply(x,table))
data.frame(do.call(rbind, lapply(r1,function(x) sapply(x,table))))
sapply(ballons,table,ballons$T)

最佳答案

您已经在使用 tidyverse,因此您可以使用 tidy 解决方案

table(ballons %>% pivot_longer(cols = !c(T)) %>% select(T,value))

关于r - 如何按列计算每组的频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68003547/

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