Obtaining Weighted Means by Row in R This handout shows how to obtain condition means for each participant, when there are repeated measures within each condition, and counterbalancing across measures. It assumes the data is in "wide format ", where all the data for a participant is contained in a single row, as in SPSS or Qualtrics output. In the example data, participants read a total of 4 stories that were each about an act of violence. In each story the main character was either a member of the participant's in-group or out-group. Participants read 2 stories in each condition, with condition counterbalanced across stories. After each story, participants rated how serious the moral transgression was on a 7 point scale. Participants were predicted to rate the transgression as being more serious when it was committed by an outgroup member. For each story there are two columns in the dataframe: one coding the condition that participant read, and the other coding the rating that participant entered. We will use the weighted.mean() function to choose which columns to average across for each participant. This handout simply describes how to average across the random variable and get a mean for each participant. There are of course other, more sophisticated ways of analyzing repeated measures designs that include more than one random variable, such as mixed effect models. 1. Load the data into R > d1 = read.table("testdata.dxt", T) > d1 pp s1.cond s1.rating s2.cond s2.rating s3.cond s3.rating s4.cond s4.rating 1 pp1 in 2 out 6 in 3 out 7 2 pp2 out 6 in 2 out 2 in 6 2. Create weights of 1's and 0's, which will determine which columns get counted > # make a table of the condition values > # we'll use this to determine which ratings to include in the calculation of each mean > weights = with(d1, cbind(s1.cond, s2.cond, s3.cond, s4.cond)) > weights s1.cond s2.cond s3.cond s4.cond [1,] 1 2 1 2 [2,] 2 1 2 1 > # the above values are based on the levels of the factors > # subtract 1 from each > weights = weights-1 > weights s1.cond s2.cond s3.cond s4.cond [1,] 0 1 0 1 [2,] 1 0 1 0 3. Create a table of all the ratings, ignoring condition for now > ratings = with(d1, cbind(s1.rating, s2.rating, s3.rating, s4.rating)) > ratings s1.rating s2.rating s3.rating s4.rating [1,] 2 6 3 7 [2,] 6 2 2 6 4. Use a for() loop to go through each row; calculate a mean for each pp from only those rows with weight =1 # first initialize the object > out.ppmeans = NULL > for (i in 1:nrow(ratings)){ out.ppmeans[i] = weighted.mean(ratings[i,], weights[i,]) } > out.ppmeans [1] 6.5 4.0 5. Create weights for the other level > weights2 = weights == 0 > weights2 s1.cond s2.cond s3.cond s4.cond [1,] TRUE FALSE TRUE FALSE [2,] FALSE TRUE FALSE TRUE 6. Use another for() loop to calculate means for each pp in the other condition > for (i in 1:nrow(ratings)){ in.ppmeans[i] = weighted.mean(ratings[i,], weights2[i,]) } > in.ppmeans [1] 2.5 4.0 Mike Amato University of Wisconsin – Madison