我正在使用"非数字参数给二元运算符"错误。 我知道这两个参数都是数字的。 奇怪的是,如果我只是单独执行该行,则计算有效,但是当我尝试将其包装在' apply'它给了我二元运算符"非数字参数#34;错误。
这是错误:
Error in IPmz * mz_winppm : non-numeric argument to binary operator
它们是数字:
> mode(IPmz)
[1] "numeric"
> mode(mz_winppm)
[1] "numeric"
这是我的代码:
FindNovelIPFeats<-function(mz_winppm,rt_win){
apply(IPFeatsin80v4.FeatsNmsTab.test,1,function(row){
IPmz<-row[['mz']]
IPrt<-row[['rt']]
mz_win<-(IPmz*mz_winppm)/1000000
Sub<-subset(feats.mutsNms.all.tab.test,mz_mid < IPmz+mz_win & mz_mid >IPmz-mz_win)
if (nrow(Sub) ==0){
return(row)
}
})
以下是数据:
dput(IPFeatsin80v4.FeatsNmsTab.test)
structure(list(mz = c(922.611, 950.641, 997.509, 1001.509, 1009.667,
1031.649), rt = c(434, 433, 136, 170, 318, 317), chrg = c("1",
"1", "1", "1", "1", "1"), colNm = c("X922.611.434.1", "X950.641.433.1",
"X997.509.136.1", "X1001.509.170.1", "X1009.667.318.1", "X1031.649.317.1"
), IPfeat = c("If_938", "If_939", "If_940", "If_941", "If_942",
"If_943")), .Names = c("mz", "rt", "chrg", "colNm", "IPfeat"), row.names = c("If_938",
"If_939", "If_940", "If_941", "If_942", "If_943"), class = "data.frame")
dput(feats.mutsNms.all.tab.test)
structure(list(mz_min = c(302.162, 730.367, 414.234, 288.193,
555.346, 260.161, 576.293, 360.324, 310.168, 378.202, 260.162,
360.197, 257.247, 391.282), mz_max = c(302.163, 730.367, 414.238,
288.2, 555.358, 260.17, 576.328, 360.328, 310.171, 378.204, 260.168,
360.199, 257.248, 391.289), rt_min = c(127, 156, 106, 443, 335,
294, 119, 424, 144, 153, 448, 138, 391, 423), rt_max = c(133,
160, 114, 537, 360, 514, 152, 428, 163, 160, 550, 150, 408, 431
), feature = c("f_25", "f_26", "f_27", "f_28", "f_29", "f_30",
"f_31", "f_32", "f_33", "f_34", "f_35", "f_36", "f_37", "f_38"
), mz_mid = c(302.1625, 730.367, 414.236, 288.1965, 555.352,
260.1655, 576.3105, 360.326, 310.1695, 378.203, 260.165, 360.198,
257.2475, 391.2855), rt_mid = c(130, 158, 110, 490, 347.5, 404,
135.5, 426, 153.5, 156.5, 499, 144, 399.5, 427), mz_rng = c(0.0010000000000332,
0, 0.0040000000000191, 0.007000000000005, 0.0119999999999436,
0.00900000000001455, 0.0349999999999682, 0.00399999999996226,
0.0029999999999859, 0.00200000000000955, 0.00600000000002865,
0.00200000000000955, 0.000999999999976353, 0.007000000000005),
rt_rng = c(6, 4, 8, 94, 25, 220, 33, 4, 19, 7, 102, 12, 17,
8), mz_rng_ppm = c(3.30947751634699, 0, 9.65633117358004,
24.2889833846178, 21.6079171407389, 34.5933646083533, 60.7311510027462,
11.1010584858219, 9.67213088322966, 5.28816535037943, 23.0622873946482,
5.55250167965827, 3.8873069708213, 17.8897505785545), metabolite = c(NA,
NA, NA, NA, "RC10C12_feats1", NA, NA, NA, NA, NA, NA, NA,
NA, NA)), .Names = c("mz_min", "mz_max", "rt_min", "rt_max",
"feature", "mz_mid", "rt_mid", "mz_rng", "rt_rng", "mz_rng_ppm",
"metabolite"), row.names = 25:38, class = "data.frame")
任何帮助表示赞赏, 感谢
答案 0 :(得分:2)
IPFeatsin80v4.FeatsNmsTab.test
包含字符数据,因此当您使用apply
时,您的data.frame将转换为字符类型的矩阵。 IPmz<-row[['mz']]
和IPrt<-row[['rt']]
属于字符类型。
因此,如果将它们转换回数字,它将起作用:
FindNovelIPFeats<-function(mz_winppm,rt_win){
apply(IPFeatsin80v4.FeatsNmsTab.test,1,function(row){
IPmz<-as.numeric(row[['mz']])
IPrt<-as.numeric(row[['rt']])
mz_win<-(IPmz*mz_winppm)/1000000
Sub<-subset(feats.mutsNms.all.tab.test,mz_mid < IPmz+mz_win & mz_mid >IPmz-mz_win)
if (nrow(Sub) ==0){
return(row)
}
})}
输出:
> FindNovelIPFeats(1,2)
If_938 If_939 If_940 If_941 If_942 If_943
mz " 922.611" " 950.641" " 997.509" "1001.509" "1009.667" "1031.649"
rt "434" "433" "136" "170" "318" "317"
chrg "1" "1" "1" "1" "1" "1"
colNm "X922.611.434.1" "X950.641.433.1" "X997.509.136.1" "X1001.509.170.1" "X1009.667.318.1" "X1031.649.317.1"
IPfeat "If_938" "If_939" "If_940" "If_941" "If_942" "If_943"
将apply
与data.frame一起使用会导致很多问题,并且所有这些转换效率都不高。也许您应该考虑使用数字列对data.frame进行子集化,然后将其转换为矩阵(无论如何您似乎不使用字符列),然后使用apply。