Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

r - "Non-numeric argument to binary operator error" when making a grouped boxplot

I am trying to create a grouped boxplot using a fairly simple, but long dataset called trch.

First I used trchtrue<- trch[complete.cases(trch), ] to create a matrix with two columns and no 'NaN' values.

> head(trchtrue)
  REACH  WTEMP
2     1 11.090
3     2 11.120
4     3 11.200
5     4  9.334
6     5  9.556
7     6  9.263

> tail(trchtrue)
        REACH WTEMP
1342315    99 0.100
1342316   100 5.131
1342317   101 0.100
1342318   102 0.100
1342319   103 0.100
1342321   105 4.994

When trying to create a grouped box plot using boxplot(trchtrue$REACH~trchtrue$WTEMP), I get the following error:

Error in x[floor(d)] + x[ceiling(d)] : non-numeric argument to binary operator

With the exception of the column titles, all of the values appear to be numeric, however, I get this result:

is.numeric(trchtrue)
[1] FALSE

I am not able to convert the matrix to numeric either.

> as.numeric(trchtrue)
Error: 'list' object cannot be coerced to type 'double'
> mode(trchtrue) = "numeric"
Error in mde(x) : 'list' object cannot be coerced to type 'double'

How can I convert this matrix to numeric so I can make the box plot?

EDIT: In the original dataset, the values were in E notation. Could this be part of the problem?

EDIT 2: Here is the reprex.

boxplot(trchtrue$REACH~trchtrue$WTEMP)
#> Error in eval(predvars, data, env): object 'trchtrue' not found

Created on 2020-12-31 by the reprex package (v0.3.0)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's another potential solution. First, transform each column to numeric. Next, plot the boxplot for the target columns.

trchtrue <- sapply( trchtrue, as.numeric )
boxplot(trchtrue$REACH~trchtrue$WTEMP)

Sometimes the as.character function is also requiered within as.numeric. This can solve the issue is the code above creates NAs

trchtrue[] <- lapply(trchtrue, function(x) as.numeric(as.character(x)))

Good luck!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...