user2716568
user2716568

Reputation: 1946

How to calculate the percentage of one column, by grouping variables, relative to another?

My (sample) data is structured as follows:

Individ <- data.frame(Participant = c("Bill", "Bill", "Bill", "Bill", "Bill", "Bill", "Bill", "Bill", "John", "John", "John", "John", 
                                      "Harry", "Harry", "Harry", "Harry","Harry", "Harry", "Harry", "Harry", "Paul", "Paul", "Paul", "Paul"),
                      Time = c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4),
                      Condition = c("Placebo", "Placebo", "Placebo", "Placebo", "Expr", "Expr", "Expr", "Expr", "Expr", "Expr", "Expr", "Expr", 
                                    "Placebo", "Placebo", "Placebo", "Placebo", "Expr", "Expr", "Expr", "Expr", "Expr", "Expr", "Expr", "Expr"),
                      Power = c(400, 250, 180, 500, 300, 450, 600, 512, 300, 500, 450, 200, 402, 210, 130, 520, 310, 451, 608, 582, 390, 570, 300, 200))

I calculate the average work completed by each Participant under each Condition using the following:

IndividAvgWork <- ddply(Individ, c("Participant", "Condition"), summarise,
                         Power = mean(Power))

I now wish to calculate the Power performed by each minute of Time in the data.frame Individ, as a percentage of the overall mean power in IndividAvgWork. This must be completed for each Participant under each Condition. Is there a quick method to do this?

An example of my anticipated output, for Bill during the Placebo condition, would be:

RelPower = c(120.30, 75.19, 54.14, 150.38)

The above was calculated by: (Sampled Power / Mean Power)*100.

As a worked example, Bill's Power at a Time of 1 under the Placebo Condition was 400. I then divided this by the mean power for Bill under the Placebo Condition which is 332.50 and stored in IndividAvgWork. Substituting these values in gives: (400/332.5)*100

Thank you.

Upvotes: 0

Views: 103

Answers (1)

Gopala
Gopala

Reputation: 10483

You can try something like this using library dplyr:

library(dplyr)
Individ %>% group_by(Participant, Condition) %>% mutate(PercentPower = round(Power / (mean(Power)) * 100, 2))

This is what it will produce:

Source: local data frame [24 x 5]
Groups: Participant, Condition [6]

   Participant  Time Condition Power PercentPower
        (fctr) (dbl)    (fctr) (dbl)        (dbl)
1         Bill     1   Placebo   400       120.30
2         Bill     2   Placebo   250        75.19
3         Bill     3   Placebo   180        54.14
4         Bill     4   Placebo   500       150.38
5         Bill     1      Expr   300        64.45
6         Bill     2      Expr   450        96.67
7         Bill     3      Expr   600       128.89
8         Bill     4      Expr   512       109.99
9         John     1      Expr   300        82.76
10        John     2      Expr   500       137.93
..         ...   ...       ...   ...          ...

Upvotes: 0

Related Questions