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 > m <- as.matrix(info); > diff(m[,0]) 4 6 7 any idea why this is happening? and how to get the correct diff. thank you --------------------------------- [[alternative HTML version deleted]]
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 matrixThe 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
What I presume is the desired answer can be arrived at via: diff(as.numeric(row.names(info))) The command: m[,0] returns a matrix with zero columns, but it still has row names. Performing 'diff' on this returns another matrix with zero columns and one less row name. Patrick Burns patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") 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 > > m <- as.matrix(info); > > diff(m[,0]) > > 4 > 6 > 7 > > any idea why this is happening? and how to get the > correct diff. > > > thank you > >--------------------------------- > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > > >