Non-Numeric Argument to Binary Operator Error in R

R using diff: non-numeric argument to binary operator error

I presume that in your tt[1,], that

class(tt[1,])
# [1] "data.frame"

So if you use as.numeric, you should be okay. Try this:

diff(as.numeric(tt[1,]))

Here's an example that we can inspect:

tt <- data.frame(x = 1, y = 2)
is.vector(tt[1,])
# [1] FALSE
class(tt[1,])
# [1] "data.frame"
diff(tt[1,])
# Error in r[i1] - r[-length(r):-(length(r) - lag + 1L)] :
# non-numeric argument to binary operator
as.numeric(tt[1,])
# [1] 1 2
diff(as.numeric(tt[1,]))
# [1] 1

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

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

Error in FUN(left, right) : non-numeric argument to binary operator

First check if the class of the data is numeric. str(NB1stRow) will show you classes of each column. If they are not numeric, turn them to numeric by -

cols <- 5:74
NB1stRow[cols] <- lapply(NB1stRow[cols], as.numeric)

Multiplication (*) doesn't require loop and it can be applied to dataframe directly so you can do

NB1stRow[cols] <- NB1stRow[cols] * 2

R error - Non-numeric Argument to Binary Operator

I have fixed your code. Now it works flawlessly.

library(tidyverse)

n = 100
linelist = tibble(
age = sample(10:40, n, replace = TRUE),
age_unit = sample(c("years", "months", NA, "other"), n, replace = TRUE)
)

linelist <- linelist %>%
mutate(age_years = case_when(
age_unit == "years" ~ as.double(age), # if age is given in years
age_unit == "months" ~ as.double(age)/12, # if age is given in months
is.na(age_unit) ~ as.double(age), # if age unit is missing, assume years
TRUE ~ NA_real_))

linelist

output

# A tibble: 100 x 3
age age_unit age_years
<int> <chr> <dbl>
1 19 NA 19
2 11 months 0.917
3 29 NA 29
4 32 months 2.67
5 29 NA 29
6 20 months 1.67
7 25 months 2.08
8 13 years 13
9 10 years 10
10 24 NA 24
# ... with 90 more rows

This works even if the age is of type chr

linelist = tibble(
age = sample(paste(10:40), n, replace = TRUE),
age_unit = sample(c("years", "months", NA, "other"), n, replace = TRUE)
)

output

# A tibble: 100 x 2
age age_unit
<chr> <chr>
1 27 years
2 26 months
3 23 years
4 30 years
5 37 months
6 17 NA
7 16 months
8 33 other
9 36 other
10 12 months

And next

linelist <- linelist %>% 
mutate(age_years = case_when(
age_unit == "years" ~ as.double(age), # if age is given in years
age_unit == "months" ~ as.double(age)/12, # if age is given in months
is.na(age_unit) ~ as.double(age), # if age unit is missing, assume years
TRUE ~ NA_real_))

linelist

output

# A tibble: 100 x 3
age age_unit age_years
<chr> <chr> <dbl>
1 27 years 27
2 26 months 2.17
3 23 years 23
4 30 years 30
5 37 months 3.08
6 17 NA 17
7 16 months 1.33
8 33 other NA
9 36 other NA
10 12 months 1
# ... with 90 more rows


Related Topics



Leave a reply



Submit