I have a follow-up question related to Calculate "group characteristics" without ddply and merge
I have a similar dataframe (per below), but trying to calculate the percentage of rotten fruits among the other fruits in the same category. I should hence not take into account whether the fruit in question itself is rotten. The dataframe per below hopefully clarifies this, the desired outcome column is purely inserted for example purposes.
Ideally i would like to use ddply (along the lines of ddply(df, .(Fruit), mutate, Perc = sum(Rotten)/length(Rotten)) ). However, i fail to find a way to find a way to only take into account the values of the other rows in the same group. I think i could use a combination of if-statements based on the values of the rows in question, but i wonder if there is a more elegant way of achieving this? Many thanks in advance, W
Fruit Rotten Desired_Outcome
1 Apple 1 0.33
2 Apple 1 0.33
3 Apple 0 0.66
4 Apple 0 0.66
5 Pear 1 0.66
6 Pear 1 0.66
7 Pear 1 0.66
8 Pear 0 1.00
9 Cherry 0 0.00
10 Cherry 0 0.00
11 Cherry 0 0.00
12 Banana 1 NA
Fruit=c(rep("Apple",4),rep("Pear",4),rep("Cherry",3),"Banana")
Gender=c(rep("Male",3),rep("Female",3))
Rotten=c(1,1,0,0,1,1,1,0,0,0,0,1)
Desired_Outcome=c(0.33,0.33,0.66,0.66,0.66,0.66,0.66,1,0,0,0,NA)
df=data.frame(Fruit,Rotten,Desired_Outcome)
df