我使用ddply data.frame按类别汇总一些,如下所示:
# with both group and size being factors / categorical
split.df <- ddply(mydata,.(group,size),summarize,
sumGroupSize = sum(someValue))
Run Code Online (Sandbox Code Playgroud)
这很顺利,但我喜欢计算比率,这意味着我需要除以组的总数.如何在同一个ddply电话中计算这样的总数?
假设我想在A组中观察到大小等级为1的观察结果.显然,我必须先计算出1级大小的所有观测值的总和.当然,我可以通过两个ddply呼叫来完成此操作,但使用所有一个呼叫会更舒服.有办法吗?
编辑:我不是故意要求过于具体,但我意识到我在这里打扰了人们.所以这是我的具体问题.事实上,我确实有一个有效的例子,但我不认为它真的很漂亮.此外,它有一个我需要克服的缺点:它无法正确应用.
library(plyr)
# make the dataset more "realistic"
mydata <- warpbreaks
names(mydata) <- c("someValue","group","size")
mydata$category <- c(1,2,3)
mydata$categoryA <- c("A","A","X","X","Z","Z")
# add some NA
mydata$category[c(8,10,19)] <- NA
mydata$categoryA[c(14,1,20)] <- NA
# someValue is summarized !
# note we have a another, varying category hence we need the a parameter
calcShares <- function(a, data) {
# !is.na needs to be specific!
tempres1 <- eval(substitute(ddply(data[!is.na(a),],.(group,size,a),summarize,
sumTest = sum(someValue,na.rm=T))),
envir=data, enclos=parent.frame())
tempres2 <- eval(substitute(ddply(data[!is.na(a),],.(group,size),summarize,
sumTestTotal = sum(someValue,na.rm=T))),
envir=data, enclos=parent.frame())
res <- merge(tempres1,tempres2,by=c("group","size"))
res$share <- res$sumTest/res$sumTestTotal
return(res)
}
test <- calcShares(category,mydata)
test2 <- calcShares(categoryA,mydata)
head(test)
head(test2)
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我打算在不同的分类变量上运行它.在示例中,我只有两个(类别,类别A),但事实上我得到了更多,所以使用我的函数应用将非常好,但不知何故它无法正常工作.
applytest <- head(apply(mydata[grep("^cat",
names(mydata),value=T)],2,calcShares,data=mydata))
Run Code Online (Sandbox Code Playgroud)
..为类别var返回一条警告信息和一个奇怪的名字(newX [,i]).
那么我怎样才能做到这一点a)更优雅,b)修复应用问题?
这看起来很简单,所以我可能遗漏了你问题的某些方面。
首先,定义一个函数来计算 的每个级别内所需的值group。.(group, size)然后,不要使用来分割 data.frame,而是使用.(group),并将新定义的函数应用到每个分割部分。
library(plyr)
# Create a dataset with the names in your example
mydata <- warpbreaks
names(mydata) <- c("someValue", "group", "size")
# A function that calculates the proportional contribution of each size class
# to the sum of someValue within a level of group
getProps <- function(df) {
with(df, ave(someValue, size, FUN=sum)/sum(someValue))
}
# The call to ddply()
res <- ddply(mydata, .(group),
.fun = function(X) transform(X, PROPS=getProps(X)))
head(res, 12)
# someValue group size PROPS
# 1 26 A L 0.4785203
# 2 30 A L 0.4785203
# 3 54 A L 0.4785203
# 4 25 A L 0.4785203
# 5 70 A L 0.4785203
# 6 52 A L 0.4785203
# 7 51 A L 0.4785203
# 8 26 A L 0.4785203
# 9 67 A L 0.4785203
# 10 18 A M 0.2577566
# 11 21 A M 0.2577566
# 12 29 A M 0.2577566
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2918 次 |
| 最近记录: |