On Sep 17, 2008, at 3:50 PM, Stacey Burrows wrote:
Dear R-users,
I have some very simple data where 1's represent events and zeroes
non-events, e.g.
temp <- rbind(c(0,1,0,0,1,1,1,0), c(0,0,0,1,0,0,0,0))
For each row in the matrix, I would like to replace a singelton
event by a 0. That is, any 1 surrounded by zeroes (010) should be
replaced by a zero (000). Sequences of 1's should be left unchanged.
So the modified matrix shoud look like this:
00001110
00000000
I had some success in identifying the non-singletons using filter(x,
c(1,1,1) and applying a test for that value being greater than 1. You
would need to disambiguate what should be happening at the end of the
series since filter() would be NA there. Adding 0 to a logical vector
returns 0's and 1's
> t(apply(temp,1, function(x) filter(x, c(1,1,1)) ) )
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] NA 1 1 1 2 3 2 NA
[2,] NA 0 1 1 1 0 0 NA
t(apply(temp,1, function(x) (filter(x, c(1,1,1)) >1)+0 ) )
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] NA 0 0 0 1 1 1 NA
[2,] NA 0 0 0 0 0 0 NA
--
David Winsemius
__________________________________________________________________
[[elided Yahoo spam]]
[[alternative HTML version deleted]]
______________________________________________
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.