[R] I need a very specific unique like function and I don't know even how to properly call this
Ista Zahn
izahn at psych.rochester.edu
Mon Nov 22 22:51:40 CET 2010
Here is a method for piecing it together using diff and indexing:
dat <- structure(c(3L, 6L, 7L, 3L, 7L, 5L, 8L, 2L, 7L, 0L, 7L, 5L, 5L,
5L, 5L, 5L, 4L, 4L, 4L, 6L), .Dim = c(10L, 2L), .Dimnames = list(
NULL, c("V1", "V2")))
diffs <- abs(diff(dat[,2], 1)) # get the difference between each
value and the previous value
new.dat <- cbind(dat, c(NA, diffs), c(diffs, NA)) # combine the diffs
with the original matrix, shifted down (is the next valued the same as
the value) and down (is the previous value the same)
new.dat <- cbind(new.dat, rowSums(new.dat[,3:4], na.rm=TRUE)) # sum
the shifted diffs so that the value is 0 if above and below are the
same, and greater than zero if the above and below values are not the
same
final.dat <- new.dat[new.dat[,5] !=0 ,1:2] # get rid of rows for
which the sum of the shifted diffs is not equal to zero.
HTH,
Ista
On Mon, Nov 22, 2010 at 8:53 PM, madr <madrazel at interia.pl> wrote:
>
> consider this matrix:
>
> [,1] [,2]
> [1,] 3 7
> [2,] 6 5
> [3,] 7 5
> [4,] 3 5
> [5,] 7 5
> [6,] 5 5
> [7,] 8 4
> [8,] 2 4
> [9,] 7 4
> [10,] 0 6
>
> I need to delete all rows where column 2 above and below has the same value,
> so the effect would be:
>
> [,1] [,2]
> [1,] 3 7
> [2,] 6 5
> [6,] 5 5
> [7,] 8 4
> [9,] 7 4
> [10,] 0 6
>
> is there a built in function for that kind of operation or I must write one
> from scratch ?
> Is there a name for that kind of operation ?
> --
> View this message in context: http://r.789695.n4.nabble.com/I-need-a-very-specific-unique-like-function-and-I-don-t-know-even-how-to-properly-call-this-tp3054427p3054427.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org
More information about the R-help
mailing list