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

We parse a CSV-File with some numbers with the following command:

tt <- read.table("test2.csv",sep=";",stringsAsFactors=FALSE)

And it works. Printingtt[1,] yields a nice vector and sd(tt[1,]) is sensible.

However, when we try

diff(tt[1,])

The command-line returns the error:

Error in r[i1] - r[-length(r):-(length(r) - lag + 1L)] :
    non-numeric argument to binary operator error

Why is that? Any ideas?

Enquirer: Joachim

||

Solution #1:

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
Respondent: Rich Scriven

Solution #2:

This also works:

for (i1 in 1:length(dat.diff))
{
diffy <- diff(as.numeric(dat.diff[,i1]))
dat.diff[,i1] <- c(diffy, NA)
}
Respondent: Starbucks

Solution #3:

I don’t think this issue is related to the class dataframe (df). The issue is that you are trying to run diff() on a possibly multi column df. Based on the documentation, the argument for diff required is a vector or matrix. I would suggest trying this:

TS_tt <- ts(tt$col, start= (the date you would like to start the time series)

then run:

adjusted_tt <- diff(tt, difference= 1)

Hope this helps.

Respondent: Sally_ar

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.