在 R 中解聚合/反向汇总/扩展数据集 [英] De-aggregate / reverse-summarise / expand a dataset in R
本文介绍了在 R 中解聚合/反向汇总/扩展数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据如下所示:
data("Titanic")
df <- as.data.frame(Titanic)
如何对计数/频率进行解聚合或反向汇总并将数据集扩展回其原始的非计数观察状态?
How can I de-aggregate or reverse-summarise count/freq and expand the data set back to it's original non-count observation state?
例如,我希望 3rd, Male, Child, No
重复 35 次,1st, Women, Adult, Yes
重复 140 次,以此类推,以此类推.数据框.
For instance, I want 3rd, Male, Child, No
repeated 35 times and 1st, Female, Adult, Yes
repeated 140 times, etc, etc, in the dataframe.
提前致谢.
推荐答案
没有包,我们可以根据给定的频率重复每一行:
Without packages we can repeat each row according to the frequencies given:
df2 <- df[rep(1:nrow(df), df[,5]),-5]
这篇关于在 R 中解聚合/反向汇总/扩展数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文