ManInMoon
ManInMoon

Reputation: 7005

Why do I get error when using diff in R on matrix

I read data in from a csv file using

d<-read.csv("myfile.csv",header=TRUE)

The data is read correctly and has 10 columns of double data with header.

I want to create a matrix(my word - not sure what I actually want in R) of the log differences.

logs<-log(d)

This works fine, but

diffs<-diff(logs)

gives this error:

-(length(r) - lag + 1L) <<< I have no idea what that means...

EDIT 1 My data (sorry had to type it here - can't see an obvious way to attach a file)

A,B,C
1.1,2.1,3.1
1.2,2.2,3.2
1.3,2.3,3.3
1.4,2.4,3.4
1.5,2.5,3.5
1.6,2.6,3.6

Upvotes: 0

Views: 4667

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226192

The key here (and your problem) is that while diff does (to my surprise) work automatically column-wise on matrices, it doesn't work on data frames, which is what you get from read.csv and which are subtly different from matrices.

d <- read.csv(textConnection("
A,B,C
1.1,2.1,3.1
1.2,2.2,3.2
1.3,2.3,3.3
1.4,2.4,3.4
1.5,2.5,3.5
1.6,2.6,3.6"))
(logs <- log(d))
diff(logs)
Error in r[i1] - r[-length(r):-(length(r) - lag + 1L)] : 
  non-numeric argument to binary operator

diff(as.matrix(logs))

My initial answer was that you should use

apply(logs,2,diff)

to explicitly use diff on each column. However, from ?diff:

x: a numeric vector or matrix containing the values to be differenced. ... If ‘x’ is a matrix then the difference operations are carried out on each column separately.

You could also do diff(c(as.matrix(logs))) if you wanted to treat the whole thing as a single continuous (column-ordered) vector.

Upvotes: 6

Related Questions