仅适用于二元运算符错误的非数字参数 [英] non-numeric argument to binary operator error ONLY within apply

查看:34
本文介绍了仅适用于二元运算符错误的非数字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了二元运算符的非数字参数"错误.我知道这两个参数都是数字.奇怪的是,如果我只是单独执行那行,计算就可以工作,但是当我尝试将它包装在应用"中时,它给了我二元运算符的非数字参数"错误.

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.

这里是错误:

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

感谢任何帮助,谢谢

推荐答案

IPFeatsin80v4.FeatsNmsTab.test 包含字符数据,所以当你使用 apply 你的 data.frame 是转换为字符类型的矩阵.IPmz<-row[['mz']]IPrt<-row[['rt']] 是字符类型.

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

输出:

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

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.

这篇关于仅适用于二元运算符错误的非数字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆