[R] Matching a pattern of vector of character strings in another vector of character strings
Petr Savicky
savicky at cs.cas.cz
Fri Dec 17 15:24:38 CET 2010
On Fri, Dec 17, 2010 at 09:34:57PM +0800, Jing Liu wrote:
>
> Dear all,
>
> My question is illustrated by the following example:
>
> I have a matrix M:
>
> > M<- matrix(c("0","0","1","1","0","1","1","0","0","*","1","1","0","1","*"),nrow=3)
> > colnames(M)<- c("2006","2007","2008","2009","2010")
> > M
> 2006 2007 2008 2009 2010
> [1,] "0" "1" "1" "*" "0"
> [2,] "0" "0" "0" "1" "1"
> [3,] "1" "1" "0" "1" "*"
>
> > pattern<- c("0","1")
>
> I would like to find, for each row, if it contains exactly the pattern of two character strings, beginning with a "0" and followed by a "1", i.e, exactly "0" "1". If it does, at which year?
> E.g. It should return 2006 for row 1, 2008 for row 2 and 2008 for row 3.
If the pattern is always c("0","1"), the number of rows is large
and the number of years is relatively small, then this may
computed also using matrix calculations. For example
M <- matrix(c("0","0","1","1","0","1","1","0","0","*","1","1","0","1","*"),nrow=3)
colnames(M) <- c("2006","2007","2008","2009","2010")
year <- colnames(M)
status <- rep(NA, times=nrow(M))
for (i in seq(length(year) - 1)) {
status[M[, i] == "0" & M[, i+1] == "1"] <- year[i]
}
status # [1] "2006" "2008" "2008"
Petr Savicky.
More information about the R-help
mailing list