在数据帧上应用适用于一行,但在多行上应用对该特定行失败
在数据帧上应用适用于一行,但在多行上应用对该特定行失败 这是一个示例(第 954 行单独工作):
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
但是当它与 data.frame 中的一个或多个其余行组合时,第 954 行返回 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
这是我正在使用的函数(请原谅) <<-,需要在单独的函数中访问这些变量!欢迎任何有关重写的建议!您可以看到我尝试重写一个行函数,但该函数不起作用并被注释掉。
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,]$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)
}
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)
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):
2571`
numeric(0)
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):
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,]$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)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定您的 SQL 查询返回您想要的结果吗? Numeric(0) 建议您检索空结果集。此外,正如@Prasad Chalasani 所指出的,尝试发布更准确的问题描述。从上面我不确定我理解你的目标是什么。
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.