R编程:plyr如何使用ddply从列中计算值
问题描述:
我想总结下面我的数据的通过/失败状态。换句话说,我想告诉每种产品/类型的合格和不合格情况的数量。R编程:plyr如何使用ddply从列中计算值
library(ggplot2)
library(plyr)
product=c("p1","p1","p1","p1","p1","p1","p1","p1","p1","p1","p1","p1","p2","p2","p2","p2","p2","p2","p2","p2","p2","p2","p2","p2")
type=c("t1","t1","t1","t1","t1","t1","t2","t2","t2","t2","t2","t2","t1","t1","t1","t1","t1","t1","t2","t2","t2","t2","t2","t2")
skew=c("s1","s1","s1","s2","s2","s2","s1","s1","s1","s2","s2","s2","s1","s1","s1","s2","s2","s2","s1","s1","s1","s2","s2","s2")
color=c("c1","c2","c3","c1","c2","c3","c1","c2","c3","c1","c2","c3","c1","c2","c3","c1","c2","c3","c1","c2","c3","c1","c2","c3")
result=c("pass","pass","fail","pass","pass","pass","fail","pass","fail","pass","fail","pass","fail","pass","fail","pass","pass","pass","pass","fail","fail","pass","pass","fail")
df = data.frame(product, type, skew, color, result)
以下CMD返回Pass总数+失败案例,但我要为通单独的列和失败
dfSummary <- ddply(df, c("product", "type"), summarise, N=length(result))
的结果是:
product type N
1 p1 t1 6
2 p1 t2 6
3 p2 t1 6
4 p2 t2 6
的desireable结果将被
product type Pass Fail
1 p1 t1 5 1
2 p1 t2 3 3
3 p2 t1 4 2
4 p2 t2 3 3
我试图像财产以后:
dfSummary <- ddply(df, c("product", "type"), summarise, Pass=length(df$product[df$result=="pass"]), Fail=length(df$product[df$result=="fail"]))
但明显因为结果是失败,并通过盛大totatl是错误的。
在此先感谢您的建议! Regards, Riad。
答
尝试:
dfSummary <- ddply(df, c("product", "type"), summarise,
Pass=sum(result=="pass"), Fail=sum(result=="fail"))
这使我产生:
product type Pass Fail
1 p1 t1 5 1
2 p1 t2 3 3
3 p2 t1 4 2
4 p2 t2 3 3
说明:
- 你给的数据集,
df
到ddply
功能。 -
ddply
是关于变量分裂,“产品”和对两个变量的每一种组合的“类型”- 这导致
length(unique(product)) * length(unique(type))
件(即,数据的子集df
)分裂。
- 这导致
- 对于每一块,
ddply
应用您提供的一些功能。在这种情况下,您可以统计result=="pass"
和result=="fail"
的数量。 - 现在
ddply
剩下一部分结果,即您分割的变量(产品和类型)和您请求的结果(通过和失败)。 - 它结合了所有的作品一起,并返回其
答
你也可以使用reshape2::dcast
。
library(reshape2)
dcast(product + type~result,data=df, fun.aggregate= length,value.var = 'result')
## product type fail pass
## 1 p1 t1 1 5
## 2 p1 t2 3 3
## 3 p2 t1 2 4
## 4 p2 t2 3 3
+0
Thx这么多!这也是工作。 – Riad
完美,这就是我需要的! Thx为即时答案! – Riad