输出格式:聚合比例计算

时间:2016-03-30 11:53:51

标签: r output

我想计算不同子集的计数,使用以下方法正常工作:

count.sur <- aggregate(Survived ~ Child + Sex, test.titanic, FUN = length)

### Child  Sex    Survived
# 1     0 female      142
# 2     1 female       10
# 3     0   male      254
# 4     1   male       12

然后我想计算这些数量的比例。

我使用了这个函数并将其添加到我的代码行中:

    prop.fnc <- function(x){
    results <- count.sur[,3]/sum(count.sur[,3]) 
    return <- results
    }

    aggregate(Survived ~ Child + Sex, test.titanic, prop.fnc)

返回的值都是正确的,但它们不是按照列组织的,而是按行排列并自行复制4次。

#   Child    Sex Survived.1 Survived.2 Survived.3 Survived.4
# 1     0 female 0.33971292 0.02392344 0.60765550 0.02870813
# 2     1 female 0.33971292 0.02392344 0.60765550 0.02870813
# 3     0   male 0.33971292 0.02392344 0.60765550 0.02870813
# 4     1   male 0.33971292 0.02392344 0.60765550 0.02870813

我不确定输出格式出错的地方。

1 个答案:

答案 0 :(得分:0)

你的函数有几个问题:x被用作参数,但从未在函数体内使用过;最后一行应为return(results),或仅results,或者您甚至可以删除所有resultsreturn<-

我认为你真的不需要aggregate,你可以简单地

count.sur$Survived.prop <- count.sur$Survived / sum(count.sur$Survived)

有一个可重复的例子:

df <- data.frame(Child=rep(0:1, 2),
                 Sex=rep(c("female", "male"), each=2),
                 Survived=c(142, 10, 254, 12))
df
Child    Sex Survived
1     0 female      142
2     1 female       10
3     0   male      254
4     1   male       12

df$Survived.prop <- with(df, Survived / sum(Survived))
Child    Sex Survived Survived.prop
1     0 female      142    0.33971292
2     1 female       10    0.02392344
3     0   male      254    0.60765550
4     1   male       12    0.02870813

这是你想要获得的吗?