gpt4 book ai didi

r - 用dplyr总结一个因子的计数

转载 作者:行者123 更新时间:2023-12-03 13:40:31 28 4
gpt4 key购买 nike

我想按列(所有者)对数据框进行分组,并输出一个新的数据框,该数据框在每次观察时都具有每种因子的计数。实际数据帧相当大,并且有10个不同的因素。

这是一些示例输入:

library(dplyr)
df = tbl_df(data.frame(owner=c(0,0,1,1), obs1=c("quiet", "loud", "quiet", "loud"), obs2=c("loud", "loud", "quiet", "quiet")))

owner obs1 obs2
1 0 quiet loud
2 0 loud loud
3 1 quiet quiet
4 1 loud quiet

我一直在寻找看起来像这样的输出:
out = data.frame(owner=c("0", "0", "1", "1"), observation=c("obs1", "obs2", "obs1", "obs2"), quiet=c(1, 0, 1, 2), loud=c(1, 2, 1, 0))

owner observation quiet loud
1 0 obs1 1 1
2 0 obs2 0 2
3 1 obs1 1 1
4 1 obs2 2 0

融化使我一路走来:
melted = tbl_df(melt(df, id=c("owner")))

owner variable value
1 0 obs1 quiet
2 0 obs1 loud
3 1 obs1 quiet
4 1 obs1 loud
5 0 obs2 loud
6 0 obs2 loud
7 1 obs2 quiet
8 1 obs2 quiet

但是最后一步是什么?如果“值”是一个数字,我就去:
melted %>% group_by(owner, variable) %>% summarise(counts=sum(value))

非常感谢!

最佳答案

2017年的答案是

library(dplyr)
library(tidyr)

gather(df, key, value, -owner) %>%
group_by(owner, key, value) %>%
tally %>%
spread(value, n, fill = 0)

给出输出
Source: local data frame [4 x 4]
Groups: owner, key [4]

owner key loud quiet
* <dbl> <chr> <dbl> <dbl>
1 0 obs1 1 1
2 0 obs2 2 0
3 1 obs1 1 1
4 1 obs2 0 2

在2019年,答案是:
gather(df, key, value, -owner) %>% 
count(owner, key, value) %>%
spread(value, n, fill = 0)

关于r - 用dplyr总结一个因子的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25811756/

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