I'm looking for a way to add a column to my data table that consists of residuals
from a lm(a~b)
function computed separately for different levels of c
I've been suggested to look into sort_by(c)
function but that doesn't seem to work with lm(a~b)
My working example data looks like this:
Columns subject, trial and rt are within a data.frame
, my goal is to compute Zre_SPSS
(that I originally made in SPSS) but from a R
function.
I've tried
data %<>% group_by (subject) %>%
mutate(Zre=residuals(lm(log(rt)~trial)))
but it doesn't work - Zre gets computed but not within each subject separately, rather for the entire data frame.
Anyone could please help me? I'm a complete R (and coding in general) newbie, so please forgive me if this question is stupid or a duplicate, chances are I didn't understand other solutions or they where not solutions I looked for. Best regards.
As per Ben Bolker request here is R code to generate data from excel screen shot
#generate data
subject<-c(1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3)
subject<-factor(subject)
trial<-c(1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6)
rt<-c(300,305,290,315,320,320,350,355,330,365,370,370,560,565,570,575,560,570)
#Following variable is what I would get after using SPSS code
ZreSPSS<-c(0.4207,0.44871,-1.7779,0.47787,0.47958,-0.04897,0.45954,0.45487,-1.7962,0.43034,0.41075,0.0407,-0.6037,0.0113,0.61928,1.22038,-1.32533,0.07806)
#make data frame
sym<-data.frame(subject, trial, rt, ZreSPSS)
tidyr::nest
and a quick blog blurb. – r2evanstrial
on the RHS, and not as a factor, doesn't seem to make much sense. Is this really the data you fit the SPSS model on? – Hong Ooi