r - Subtract mean from individual response, across separate data frames -
my data structure
my experiment effect of condition (placebo or experimental) on power produced individuals during 3 sprint efforts, measured across 2 seperate sessions.
1 data.frame
containes individual responses (individ)
, structured as:
individ <- data.frame(subjectid = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4), sprint = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3), session = c(1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2), condition = c(placebo, placebo, placebo, placebo, placebo, placebo, exper, exper, exper, exper, exper, exper, placebo, placebo, placebo, placebo, placebo, placebo, exper, exper, exper, exper, exper, exper), power = c(400, 250, 180, 500, 300, 450, 600, 512, 300, 500, 450, 200, 460, 254, 183, 540, 360, 420, 610, 514, 307, 508, 454, 201))
i obtained mean power each session via library(dplyr)
, following code second data.frame
:
averagenmt <- summarise(group_by(individ, session, condition), mean(power, na.rm = true))
my problem
i wish calculate deltas each individual's session. how subtract mean power, grouping session , condition, , adding in new column? expected output be:
deltascores <- data.frame(subjectid = c(1, 1, 2, 2, 3, 3, 4, 4), session = c(1, 2, 1, 2, 1, 2, 1, 2), condition = c(placebo, placebo, exper, exper, placebo, placebo, exper, exper), deltapower = c(-11.1667, -11.6667, -3.16667, 11.16667, 11.66667, 189.1667, 2.166667))
do need use dplyr::mutate()
?
assistance appreciated.
there may more elegant solution, need merge in summarized data able calculate on it. clean syntax via %>%
averagenmt <- individ %>% group_by(session, condition) %>% summarize(mean_power= mean(power, na.rm=true)) ind2 <- merge(individ, averagenmt, by= c("session", "condition")) # name whatever want ind2 %>% group_by(subjectid, condition, session) %>% summarize(deltapower= mean(power) - mean(mean_power)) source: local data frame [8 x 4] groups: subjectid, condition subjectid condition session deltapower 1 1 placebo 1 -11.166667 2 1 placebo 2 -11.666667 3 2 exper 1 -3.166667 4 2 exper 2 -2.166667 5 3 placebo 1 11.166667 6 3 placebo 2 11.666667 7 4 exper 1 3.166667 8 4 exper 2 2.166667
Comments
Post a Comment