I have a data set where I want the correlations between 2 variables
conditional on a students grade level.

This code works just fine.

by(tmp[,c('mtsc07', 'DCBASmathscoreSPRING')], tmp$Grade, cor,
use='complete', method='pearson')

However, this generates an error

by(tmp[,c('mtsc07', 'DCBASmathscoreSPRING')], tmp$Grade, cor,
use='complete', method='spearman')
Error in FUN(data[x, ], ...) : 'x' is empty

I can subset the data by grade and compute spearman rho as

tmp5 <- subset(tmp, Grade == 5)
cor(tmp5[,c('mtsc07', 'DCBASmathcountSPRING')], use='complete',
method='spearman')

But doing this iteratively is inefficient.

I don't see anything in the help man for by() or cor() that tells me
what the problem is. I might be missing it though. Any thoughts?

Thanks,
Harold


> sessionInfo()
R version 2.5.0 (2007-04-23) 
i386-pc-mingw32 

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
States.1252;LC_MONETARY=English_United
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] "stats"     "graphics"  "grDevices" "utils"     "datasets"
"methods"   "base"     

other attached packages:
 lattice 
"0.15-4" 


	[[alternative HTML version deleted]]

