R使用差异:二元运算符错误的非数字参数 [英] R using diff: non-numeric argument to binary operator error
本文介绍了R使用差异:二元运算符错误的非数字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们使用以下命令解析包含一些数字的 CSV 文件:
We parse a CSV-File with some numbers with the following command:
tt <- read.table("test2.csv",sep=";",stringsAsFactors=FALSE)
它有效.打印tt[1,]
产生一个很好的向量,sd(tt[1,])
是明智的.
And it works. Printingtt[1,]
yields a nice vector and sd(tt[1,])
is sensible.
然而,当我们尝试
diff(tt[1,])
命令行返回错误:
Error in r[i1] - r[-length(r):-(length(r) - lag + 1L)] :
non-numeric argument to binary operator error
这是为什么?有什么想法吗?
Why is that? Any ideas?
推荐答案
我认为在你的 tt[1,]
中,那
I presume that in your tt[1,]
, that
class(tt[1,])
# [1] "data.frame"
所以如果你使用 as.numeric
,你应该没问题.试试这个:
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
这篇关于R使用差异:二元运算符错误的非数字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文