non-numeric argument to binary operator error ONLY

2019-07-19 07:20发布

问题:

I am having the "non-numeric argument to binary operator" error. I know that both of the arguments are numeric. Strangely, the calculation works if I just execute that line alone, but when I try and wrap it within 'apply' it gives me the "non-numeric argument to binary operator" error.

Here is the error:

Error in IPmz * mz_winppm : non-numeric argument to binary operator

They are numeric:

> mode(IPmz)
[1] "numeric"
> mode(mz_winppm)
[1] "numeric"

Here is my code:

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)
    }
  })

Here is the data:

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")

Any help appreciated, Thanks

回答1:

IPFeatsin80v4.FeatsNmsTab.test contains character data, so when you use apply your data.frame is converted into a matrix of character type. IPmz<-row[['mz']] and IPrt<-row[['rt']] are of type character.

So, if you convert them back to numeric it will work:

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)
    }
  })}

Output:

> 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"     

Using apply with a data.frame causes lot of problems like that and all these conversions are not efficient. Maybe you should consider subsetting your data.frame with the numeric columns plus converting it into a matrix (you don't seem to use the character ones anyway) and then use apply.



标签: r apply