Rahul Raoniar
Rahul Raoniar

Reputation: 17

How to implement the ddply of plyr package code to dplyr of the following cabbage_exp data set. Any help would be appriciated

### This is a R related problem.
### I'm using windows 10
### Latest R version and Packages

library(plyr)

library(dplyr)

library(gcookbook)

In gcookbook library there is a data set called cabbage_exp. In this data set I would like to calculate the proportion of weight within each group(grouped by Date).

In my data set 6 samples are there and each sample contains date and weight. there are 3 dates d16, d20 and d21 and each date row has a weight value. So my goal is to group the data by date and calculate the relative proportion weight. For example the group would be (d16, d16), (d20, d20) and (d21, d21) and the relative proportion of weight withing each group should be added up-to 100. Say d16 = 40% and d16 = 60% so the group total =100% and so on.

The following code of plyr package splitting on "Date" and compute weight proportion in each group. It is working like a charm but I am trying to implement this same code using dplyr package but not getting the same result

library(plyr)
ddply(cabbage_exp, "Date", transform,
      percent_weight = Weight / (Weight)*100)

Final Result of ddply (click here to see the image of plyr code results)

I'm using the following code with dplyr but getting ungrouped proportion which is not date wise grouped proportion

library(dplyr)
cabbage_exp %>% 
  group_by(Date) %>% 
  mutate(percent = Weight/sum(Weight))

Final result of dplyr code (click here to see the dplyr code results

Question.

How to implement dplyr code to get the same result. I have tried with the group_by but it isn't grouping the Date together. Any help in this problem would be appreciated.

Upvotes: 0

Views: 199

Answers (1)

B Williams
B Williams

Reputation: 2050

You can use the ungroup function.

cabbage_exp %>% 
    group_by(Date) %>% 
    mutate(Wt = sum(Weight)) %>% 
    ungroup %>% 
    mutate(percent_weight = Weight / Wt * 100) %>%
    arrange(Date) %>% 
    dplyr::select(-Wt)

Produces the following output

# A tibble: 6 x 7
  Cultivar   Date Weight        sd     n         se percent_weight
    <fctr> <fctr>  <dbl>     <dbl> <int>      <dbl>          <dbl>
1      c39    d16   3.18 0.9566144    10 0.30250803       58.45588
2      c52    d16   2.26 0.4452215    10 0.14079141       41.54412
3      c39    d20   2.80 0.2788867    10 0.08819171       47.37733
4      c52    d20   3.11 0.7908505    10 0.25008887       52.62267
5      c39    d21   2.74 0.9834181    10 0.31098410       65.08314
6      c52    d21   1.47 0.2110819    10 0.06674995       34.91686

Upvotes: 2

Related Questions