本文介绍了二进制运算符错误的非数字参数仅在apply之内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我遇到二进制运算符的非数字参数"错误.我知道两个参数都是数字.奇怪的是,如果我只执行该行,则该计算有效,但是当我尝试将其包装在"apply"中时,会出现二进制运算符的非数字参数"错误.

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

任何帮助表示赞赏,谢谢

Any help appreciated,Thanks

推荐答案

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.

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

1403页,肝出来的..

09-06 10:33