For some reason when I apply a function to a single row in R it works, but when that same row is in a data.frame it does not, see below: apply(dx[954,], 1, query.db) 2571 1092 0.06044147 3890 0.05948577 3273 0.05911261 794 0.05855583 4241 0.05854064 3560 0.05602690 apply(dx[954:955,], 1, query.db) $`2571` numeric(0) $`1194` 908 3754 5119 2835 3139 2775 0.028935504 0.026941886 0.024762815 0.023335049 0.021892711 0.021001654 1101 1762 401 3899 3727 4248 0.020434257 0.020413724 -0.083333333 0.020111260 0.019872379 0.019840807 3282 4585 798 5249 836 3570 0.019729315 0.019524213 0.019440097 0.019410513 0.019288864 0.019052412 726 3663 4775 3030 4938 3259 0.018894556 0.018838258 0.018749506 -0.083333333 0.018416353 0.018240173 1426 4563 3871 958 1242 1033 Here's the function code: calc.enrichment <- function(cptdf){ if(cptdf[4] %in% cptcodes[,1]){ P.hit <- as.numeric(cptdf[6]) / sum.all.hits return (P.hit) } else{ return (-miss) } } query.db <- function(dxdf){ snquery = dbSendQuery(connection, paste("select distinct cpt4code from DxRxLookup where icd9code = '", dxdf[4], "'", sep = "")) cptcodes <<- dbGetResult(snquery) #restrict this to constrain by Rx1 and Rx2 inputs h1 <<- which(cpt$Cpt %in% cptcodes[,1]) y <<- which((cpt$Rx2 %in% dxdf[2] | cpt$Rx1 %in% dxdf[2]) & (cpt$Rx1 %in% dxdf[3] | cpt$Rx2 %in% dxdf[3])) hits <<- intersect(y, h1) sum.all.hits <<- sum(cpt[hits,]$Log.Odds.Ratio) miss <<- 1 / (length(y) - length(hits)) apply(cpt[y,], 1, calc.enrichment) } Thanks for any help! [[alternative HTML version deleted]]