apply on dataframe works for one row, but apply over multiple rows fails for that particular row
Apply on dataframe works for one row, but apply over multiple rows fails for that particular row Here's an example (row 954 works in isolation):
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
But when it's combined with one or more of the rest of the rows in the data.frame, row 954 returns numeric(0):
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 are the functions I'm using (please forgive the <<-, needed access to those variables in separate functions! Any suggestions on re-writing that are welcome! You can see my attempt at re-writing an an-line function which did not work and is commented out.
query.db <- function(dxdf){
snquery = dbSendQuery(connection, paste("select distinct cpt4code from DxRxLookup where icd9code = '", dxdf[4], "'", sep = ""))
cptcodes <<- dbGetResult(snquery)
if(dim(cptcodes)[1] == 0){
return (0)
}
#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,开发者_如何学C]$Log.Odds.Ratio)
miss <<- 1 / (length(y) - length(hits))
# apply(cpt[y,], 1, function(cptdf) if (cptdf[4] %in% codes[,1]){ P.hit <- as.numeric(cptdf[6]) / sum.of.hits; return (P.hit)} else { return (-m)}, c(codes=cptcodes, sum.of.hits=sum.all.hits, m=miss))
apply(cpt[y,], 1, calc.enrichment)
}
Are you sure your SQL query returns what you would like? Numeric(0) suggest that you retrieve an empty resultset. Furthermore, as pointed by @Prasad Chalasani, try to post a more precise problem description. From the above I am not sure I understand what your aim is.
精彩评论