[R] I need a very specific unique like function and I don't know even how to properly call this
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@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.
Re: [R] I need a very specific unique like function and I don't know even how to properly call this
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 madra...@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@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 __ R-help@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.
Re: [R] I need a very specific unique like function and I don't know even how to properly call this
Given a vector, x, we can test if the value above it is equal to itself with abv = c(FALSE,x[-l] == x[-1]) and if the value below is equal to itself with blw = c(x[-l] == x[-1],FALSE) So, for your problem: abv = c(FALSE,dat[,2][-l] == dat[,2][-1]) blw = c(dat[,2][-l] == dat[,2][-1],FALSE) dat[!(abv blw),] [,1] [,2] [1,]37 [2,]65 [3,]55 [4,]84 [5,]74 [6,]06 - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spec...@stat.berkeley.edu On Mon, 22 Nov 2010, madr 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@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. __ R-help@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.