"Non-numeric argument to binary operator error" 制作分组箱线图时

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

我正在尝试使用名为 trch.

的相当简单但很长的数据集来创建分组箱线图

首先,我使用 trchtrue<- trch[complete.cases(trch), ] 创建了一个包含两列且没有 'NaN' 值的矩阵。

> 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

尝试使用 boxplot(trchtrue$REACH~trchtrue$WTEMP) 创建分组箱线图时,出现以下错误:

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

除了列标题外,所有值似乎都是数字,但是,我得到了这个结果:

is.numeric(trchtrue)
[1] FALSE

我也无法将矩阵转换为数字。

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

如何将此矩阵转换为数字以便绘制箱线图?

编辑:在原始数据集中,值采用 E 表示法。这可能是问题的一部分吗?

编辑 2:这是代表。

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

reprex package (v0.3.0)

于 2020-12-31 创建

这是另一个可能的解决方案。首先,将每一列转换为数字。接下来,绘制目标列的箱线图。

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

有时 as.character 函数在 as.numeric 中也需要。这可以解决问题是上面的代码创建 NAs

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

祝你好运!