gpt4 book ai didi

r - Dplyr : How can i calculate proportional means?

转载 作者:行者123 更新时间:2023-12-05 09:26:12 24 4
gpt4 key购买 nike

假设我有一个如下所示的数据集:

> dput(df)
structure(list(Fruit = c("Apple", "Apple", "Apple", "Apple",
"Apple", "Apple", "Apple", "Apple", "Apple", "Apple", "Orange",
"Banana", "Banana"), Status = c("Eaten", "Eaten", "Eaten", "Trashed",
"Eaten", "Trashed", "Eaten", "Eaten", "Trashed", "Eaten", "Eaten",
"Trashed", "Trashed")), class = "data.frame", row.names = c(NA,
-13L))

只要看一下,我们就会发现 Apples 的 Trashed 数量最多,但这是因为它也出现得最频繁​​。另一方面,我们看到 Banana 被 Trashed 的概率为 100%。

哪个函数可以让我看到哪种水果按比例被丢弃最多?例如,输出应该是:香蕉 - 100% 垃圾Orange-0% 垃圾Apple-30% 已损坏

我也想为此创建一个可视化。

谢谢!

最佳答案

基本的 base-r 解决方案是 tapply 用于计算,barplot 用于绘图:

out <- tapply(df$Status == "Trashed", df$Fruit, mean) * 100
barplot(out)

enter image description here

关于r - Dplyr : How can i calculate proportional means?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74099978/

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