二元运算符的非数字参数,CSV

Non-numeric argument to binary operator, CSV

我之前看到其他人已经在为此苦苦挣扎,但是我没能通过这些帖子解决我的问题。我收到错误 'Non-numeric argument to binary operator'。以下可重现的示例有效:

x=rnorm(1000)+sin(c(1:1000)/100)#random data+ sinus superimposed
par(mfrow=c(2,2))

plot(x)# plot random data
plot(filter(x,rep(1/100,100)))
plot(x-filter(x,rep(1/100,100)))

# variances of variable, long term variability and short term variability
var(x)
var(filter(x, rep(1/100,100)),na.rm=T)
var(x-filter(x, rep(1/100,100)),na.rm=T)

不过,我当然想用自己的数据集,它是一个csv,这时候就报错了。肯定和数据格式有关,因为当我把随机数据导出到csv时:

x=rnorm(1000)+sin(c(1:1000)/100)#random data+ sinus superimposed
write.csv(x,"dat.csv")

然后尝试读入 dat.csv

y <- read.csv("dat.csv", header=TRUE, stringsAsFactors=FALSE)

par(mfrow=c(2,2))
plot(y)
plot(filter(y,rep(1/100,100)))
plot(y-filter(y,rep(1/100,100)))

[...] 我收到错误

Error in x - filter(x, rep(1/100, 100)) : 
  non-numeric argument to binary operator
Calls: plot
In addition: Warning message:
In plot(x - filter(x, rep(1/100, 100))) :
  Incompatible methods ("Ops.data.frame", "Ops.ts") for "-"
Execution halted

为什么值不是数字?我不明白。感谢您的帮助!

我稍微重写了 post,这样 x 变量就不会被重新用于输入和输出。 read.csv() 的值现在是 y。注意它是 data.frame, while x is an ordinary numeric vector.

为了让第二组图表现得像第一组一样,从 y 中提取第一个向量(下面称为 y1),然后将该向量传递给 dplyr 函数。

y <- read.csv("dat.csv", header=TRUE, stringsAsFactors=FALSE)
y1 <- y$x # Extract the first column
par(mfrow=c(2,2))
plot(y1)
plot(filter(y1,rep(1/100,100)))
plot(y1-filter(y1,rep(1/100,100)))