Here is a (shortened) sample from a data set I am working on. The sample represents data from an experiment with 2 sessions (session_number
), in each session participants completed 5 trials (trial_number
) of a hand grip exercise (so, 10 in total; 2 * 5 = 10). Each of the 5 trials has 3 observations of hand grip strength (percent_of_maximum
). I want to get the average (below, I call it mean_by_trial
) of these 3 observations for each of the 10 trials.
Finally, and this is what I am stuck on, I want to output a data set that is 20 rows long (one row for each unique trial, there are 2 participants and 10 trials for each participant; 2 * 10 = 20), AND retains all other variables. All the other variables (in the example there are: placebo
, support
, personality
, and perceived_difficulty
) will be the same for each unique Participant
, trial_number
, or session_number
(see sample data set below).
I have tried this using ddply
, which is pretty much what I want, but the new data set does not contain the other variables in the data set (new_dat
only contains trial_number
, session_number
, Participant
and the new mean_by_trial
variable). How can I maintain the other variables?
#create sample data frame
dat <- data.frame(
Participant = rep(1:2, each = 30),
placebo = c(replicate(15, "placebo"), replicate(15, "control"), replicate(15, "control"), replicate(15, "placebo")),
support = rep(sort(rep(c("support", "control"), 3)), 10),
personality = c(replicate(30, "nice"), replicate(30, "naughty")),
session_number = c(rep(1:2, each = 15), rep(1:2, each = 15)),
trial_number = c(rep(1:5, each = 3), rep(1:5, each = 3), rep(1:5, each = 3), rep(1:5, each = 3)),
percent_of_maximum = runif(60, min = 0, max = 100),
perceived_difficulty = runif(60, min = 50, max = 100)
)
#this is what I have tried so far
library(plyr)
new_dat <- ddply(dat, .(trial_number, session_number, Participant), summarise, mean_by_trial = mean(percent_of_maximum), .drop = FALSE)
I want new_dat
to contain all the variables in dat
, plus the mean_by_trial
variable. Thank you!