[R] problem with diff

Duncan Murdoch murdoch at stats.uwo.ca
Sat Mar 25 12:46:12 CET 2006


On 3/25/2006 4:05 AM, Fred J. wrote:
> dear R users
>  
>  I am having a problem with the output of diff for a numeric vector
>  which has the values (2,4,6,7), one expects to get 2 2 1 but I am
>  getting 4 6 7, here is the setup.
>  
>  
>  info <- with(rle(x),
>       data.frame(value = values,
>                  start = cumsum(lengths)-lengths+1,
>                  end = cumsum(lengths)
>                  )[lengths > 1,]
>       );
>  > x <- c(3,4,4,4,4,5,7,7,0,1,1,2,2,2);
>  > info
>    value start end
>  2     4     2   5
>  4     7     7   8
>  6     1    10  11
>  7     2    12  14
>  
>  #since I cann't get the first column of info, I cast
>  info into a matrix

The column labelled "value" is the first column of info.  The numbers 2 
4 6 7 are the rownames, they aren't a column at all.

>  > m <- as.matrix(info); 
>  > diff(m[,0])

m[,0] gives a matrix with no columns, but it still has the same 
rownames.  I can see why you thought you were addressing the first 
column, but that's not what you were actually doing.

>  
>  4
>  6
>  7
>  
>  any idea why this is happening? and how to get the 
>  correct diff.

When diff operates on a matrix, it throws away the first rowname, and 
assigns the other rownames to the result.  Since you used it on a matrix 
with no columns, it doesn't do anything else in your case.

To get what you want, use

diff(as.numeric(rownames(info)))

Duncan Murdoch




More information about the R-help mailing list