2015-12-02 56 views
2

我有「二進制運算符的非數字參數」錯誤。 我知道這兩個參數都是數字。 奇怪的是,計算工程,如果我只是執行該行,但當我嘗試和包裝它'申請'它給了我「二進制運算符的非數字參數」錯誤。僅適用於二元運算符錯誤的非數字參數

以下是錯誤:

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

'nrow(Sub == 0)'應該是'nrow(Sub)== 0'。很常見的印刷錯誤。 – LyzandeR

+0

謝謝,哎呀,將修復,不幸的是我仍然有關於二進制運算符 – user2814482

+0

您的代碼不完整的錯誤。函數調用如何? – jogo

回答

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。

相關問題