Home > Mobile >  Numeric data gives error non-numeric argument to binary operator in R
Numeric data gives error non-numeric argument to binary operator in R

Time:04-04

New to R.

I have a dataframe called SortedDF with 125143 observations and 5 variables

Head(SortedDF)
      number Retention time (min) Charge      m/z              Group
28637  98481             16.87978      2 350.1859 Progenesis_peptide
82465 DVQLPK             14.35000      2 350.2022      PEAKS_peptide
81468 DVQLPK             14.32000      2 350.2027      PEAKS_peptide
76662 DVQLPK             14.33000      2 350.2028      PEAKS_peptide
77423 DVQLPK             14.36000      2 350.2029      PEAKS_peptide
73768 DVQLPK             14.27000      2 350.2039      PEAKS_peptide

I want to match peptides based on their m/z similarity using lead command.

MatchedDF <- SortedDF %>% mutate(matches_with_next_row = (abs(("m/z") - lead("m/z")) < 0.01))

While coding another database, this code worked perfectly. However right now, I get the error message saying

Error in mutate(): ! Problem while computing matches_with_next_row = ... & Group != lead(Group). Caused by error in ("m/z") - lead("m/z"): ! non-numeric argument to binary operator Run rlang::last_error() to see where the error occurred.

When checking

class(SortedDF$"m/z")

(1) "numeric"

I checked following threads without succes: x non-numeric argument to binary operator while using mutate --> tried '' around m/z without succes

Other threads mainly talk about other problems than mine. If anyone has an idea on what I am doing wrong, please tell me.

CodePudding user response:

"m/z" is a non-numeric character, whereas m/z with back ticks refers to a data variable of the particular column:

MatchedDF <- SortedDF %>% mutate(
  matches_with_next_row = (abs((`m/z`) - lead(`m/z`)) < 0.01)
)

It is recommended to not use special characters for column names to make these expressions much easier to write.

  • Related