Compute the correlation by looping through factor levels in a dataframe
You can use by
in base R.
subdat <- dat[order(dat$T), c("y", "F", "Subj")]
by(subdat, subdat$Subj, function(x) with(x, cor(y[F == "f1"], y[F == "f2"])))
Output
subdat$Subj: S1
[1] -0.03755675
---------------------------------------------------------------------------------
subdat$Subj: S10
[1] -0.05481364
---------------------------------------------------------------------------------
subdat$Subj: S2
[1] 0.2822211
---------------------------------------------------------------------------------
subdat$Subj: S3
[1] 0.2671967
---------------------------------------------------------------------------------
subdat$Subj: S4
[1] 0.1268404
---------------------------------------------------------------------------------
subdat$Subj: S5
[1] 0.0374699
---------------------------------------------------------------------------------
subdat$Subj: S6
[1] 0.5655247
---------------------------------------------------------------------------------
subdat$Subj: S7
[1] 0.2141196
---------------------------------------------------------------------------------
subdat$Subj: S8
[1] 0.250178
---------------------------------------------------------------------------------
subdat$Subj: S9
[1] 0.1370734