7

我想知道是否有比我通常采用的方法更直接的方法来计算某种类型的变量......

下面的示例可能最好地说明了这一点。我有一个包含 2 列的数据框(水果以及水果是否腐烂)。我想为每一行添加例如腐烂的同一类别的水果的百分比。例如,apple 有 4 个条目,其中 2 个是 rotten,因此 apple 的每一行应为 0.5。目标值(仅作为说明)包含在“期望结果”列中。

我以前通过 * 对水果变量使用“ddply”命令(以 sum/lenght 作为函数)来解决这个问题,创建一个新的 3*2 数据帧 *使用“合并”命令将这些值链接回旧数据帧.

这感觉像是一种迂回的方式,我想知道是否有更好/更快的方式来做到这一点!理想情况下是一种通用方法,如果需要确定是否所有水果都腐烂,任何水果都腐烂等等等等,而不是百分比,那么它很容易调整。

提前谢谢了,

W

    Fruit Rotten Desired_Outcome_PercRotten
1   Apple      1                        0.5
2   Apple      1                        0.5
3   Apple      0                        0.5
4   Apple      0                        0.5
5    Pear      1                       0.75
6    Pear      1                       0.75
7    Pear      1                       0.75
8    Pear      0                       0.75
9  Cherry      0                          0
10 Cherry      0                          0
11 Cherry      0                          0

#create example datagram; desired outcome columns are purely inserted as illustrative of target outcomes
Fruit=c(rep("Apple",4),rep("Pear",4),rep("Cherry",3))
Rotten=c(1,1,0,0,1,1,1,0,0,0,0)
Desired_Outcome_PercRotten=c(0.5,0.5,0.5,0.5,0.75,0.75,0.75,0.75,0,0,0)
df=as.data.frame(cbind(Fruit,Rotten,Desired_Outcome_PercRotten))        
df
4

4 回答 4

11

ddply你可以用和来做到这一点mutate

# changed summarise to transform on joran's suggestion
# changed transform to mutate on mnel's suggestion :)
ddply(df, .(Fruit), mutate, Perc = sum(Rotten)/length(Rotten))

#     Fruit Rotten Perc
# 1   Apple      1 0.50
# 2   Apple      1 0.50
# 3   Apple      0 0.50
# 4   Apple      0 0.50
# 5  Cherry      0 0.00
# 6  Cherry      0 0.00
# 7  Cherry      0 0.00
# 8    Pear      1 0.75
# 9    Pear      1 0.75
# 10   Pear      1 0.75
# 11   Pear      0 0.75
于 2013-03-17T23:14:48.093 回答
10

data.table超级快,因为它通过引用更新。使用它怎么样?

library(data.table)

dt=data.table(Fruit,Rotten,Desired_Outcome_PercRotten)

dt[,test:=sum(Rotten)/.N,by="Fruit"]
#dt
#     Fruit Rotten Desired_Outcome_PercRotten test
# 1:  Apple      1                       0.50 0.50
# 2:  Apple      1                       0.50 0.50
# 3:  Apple      0                       0.50 0.50
# 4:  Apple      0                       0.50 0.50
# 5:   Pear      1                       0.75 0.75
# 6:   Pear      1                       0.75 0.75
# 7:   Pear      1                       0.75 0.75
# 8:   Pear      0                       0.75 0.75
# 9: Cherry      0                       0.00 0.00
#10: Cherry      0                       0.00 0.00
#11: Cherry      0                       0.00 0.00
于 2013-03-17T23:16:34.037 回答
5

基础 R 中的一种解决方案是使用ave.

within(df, {
  ## Because of how you've created your data.frame
  ##   Rotten is actually a factor. So, we need to
  ##   convert it to numeric before we can use mean
  Rotten <- as.numeric(as.character(Rotten))
  NewCol <- ave(Rotten, Fruit)
})
    Fruit Rotten Desired_Outcome_PercRotten NewCol
1   Apple      1                        0.5   0.50
2   Apple      1                        0.5   0.50
3   Apple      0                        0.5   0.50
4   Apple      0                        0.5   0.50
5    Pear      1                       0.75   0.75
6    Pear      1                       0.75   0.75
7    Pear      1                       0.75   0.75
8    Pear      0                       0.75   0.75
9  Cherry      0                          0   0.00
10 Cherry      0                          0   0.00

或更短:

transform(df, desired = ave(Rotten == 1, Fruit))

应用的默认函数avemean,因此我没有在此处包含它。FUN = some-function-here但是,如果您想做一些不同的事情,您可以通过附加来指定不同的函数。

于 2013-03-18T04:44:36.000 回答
2

正如ave已经发布的那样,让我使用我选择的基本 R 函数添加一个解决方案:aggregate.

您可以通过以下方式获得所需的数据:

aggregate(as.numeric(as.character(Rotten)) ~ Fruit, df, mean)

但是,您需要在merge之后(或一件)将其静止:

merge(df, aggregate(as.numeric(as.character(Rotten)) ~ Fruit, df, mean))
于 2013-03-18T13:24:59.527 回答