gpt4 book ai didi

r - 从频率表生成 data.frame

转载 作者:行者123 更新时间:2023-12-04 11:00:54 25 4
gpt4 key购买 nike

我在 2*4 数组中有 500 个观察值的合成数据:

datax = array(c(120, 181, 50, 43, 41, 33,24,8), dim=c(2,4))
dimnames(datax) = list(gender= c('male', 'female')
, punishment = c('None', 'Community_service', 'Youth_prison', 'Normal_prison'))

我想生产一个 data.frame来自代表频率表“来源”的表。

我可以通过“频率”列( as.data.frame(as.table(datax)) ,也 here )来表示它,但我想生成具有 500 行和 2 列(性别、惩罚)的 data.frame。

我将如何在 R 中做到这一点?

最佳答案

尝试这个:

long <- as.data.frame.table(datax)
longer <- long[rep(1:nrow(long), long$Freq), -3]

关于r - 从频率表生成 data.frame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54787908/

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