Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何将ddply与变量一起使用?_R_Plyr - Fatal编程技术网

R 如何将ddply与变量一起使用?

R 如何将ddply与变量一起使用?,r,plyr,R,Plyr,我使用ddply按不同类别总结了一些数据。frame,如下所示: # with both group and size being factors / categorical split.df <- ddply(mydata,.(group,size),summarize, sumGroupSize = sum(someValue)) 正如你们所看到的,我打算在不同的分类变量上运行它。在这个例子中,我只有两个(category,categoryA),但

我使用ddply按不同类别总结了一些
数据。frame
,如下所示:

# with both group and size being factors / categorical
split.df <- ddply(mydata,.(group,size),summarize,
                  sumGroupSize = sum(someValue))
正如你们所看到的,我打算在不同的分类变量上运行它。在这个例子中,我只有两个(category,categoryA),但事实上我得到了更多,所以将apply与我的函数一起使用会非常好,但不知何故它不能正常工作

applytest <- head(apply(mydata[grep("^cat",
             names(mydata),value=T)],2,calcShares,data=mydata))   

applytest这似乎很简单,所以我可能遗漏了您问题的某些方面

首先,定义一个函数,用于计算
的每个级别内所需的值。然后,不要使用
(组,大小)
分割data.frame,而是使用
(组)
,并将新定义的函数应用于每个分割的片段

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
库(plyr)
#使用示例中的名称创建数据集

我的数据很好,我总是用另一个ddply包装ddply,就像你说的,所以我也对这个问题的解决方案感兴趣。
table
+
prop.table
+
addmargins
是否适合您,或者您是否需要增加ddply的灵活性?您是否会使用
count
功能?我的记忆是,它只是对
length
的重命名,需要在base R.Hmm中使用
ave
。很好,我不知道prop.table,但我仍然可以利用ddply的灵活性。@DWin但是当您想要的总数超过ddply目前必须处理的小部分(例如grouplevel1:sizelevel1)时,该怎么办?就像这个例子一样,为了计算比例,我们需要SizeLevel 1的整个计数。我认为这正是ran2想要的,为了做到这一点,我一直需要做两次ddply,或者更简单地使用
函数族。+1用于将一些新的想法(使用/ave/transform)带到表中。我意识到,我有点不精确,并张贴了一个更具体的可复制的例子。您的解决方案缺少的是摘要(即聚合)。但也许你可以把它添加到你的解决方案中。我不是只关注ddply:)我也喜欢这样,但我不知道这是否比一开始就做2倍ddply更简单。
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