Skip to content

Matching a pattern of vector of character strings in another vector of character strings

6 messages · Jing Liu, Liviu Andronic, Marc Schwartz +3 more

#
On Fri, Dec 17, 2010 at 2:34 PM, Jing Liu <quiet_jing0920 at hotmail.com> wrote:
I could only think of this
[1] 1 1 1
[1] TRUE TRUE TRUE

But it doesn't return the position of the matched string. So this
isn't what you wanted.

Regards
Liviu

  
    
  
#
On Dec 17, 2010, at 7:58 AM, Liviu Andronic wrote:

            
Try this:
[1] "2006" "2008" "2008"


See ?regexpr for more info.

HTH,

Marc Schwartz
#
On Fri, Dec 17, 2010 at 09:34:57PM +0800, Jing Liu wrote:
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.
#
On Dec 17, 2010, at 8:34 AM, Jing Liu wrote:

            
You can just paste() each row with collapse="._" and now can use grep- 
ish functions as you were hoping to use.

 > m2 <- apply(M, 1, paste, collapse="_")
 > colnames(M)[(regexpr("0_1", m2)+1)/2]  # assuming number of  
characters per element are all 1
[1] "2006" "2008" "2008"
#
On Fri, Dec 17, 2010 at 9:10 AM, Marc Schwartz <marc_schwartz at me.com> wrote:
Here is a slight variation which would only be needed if its possible
that a row can have no 01 at all:

ix <- regexpr("01", apply(M, 1, paste, collapse = ""))
colnames(M)[ ifelse(ix > 0, ix, NA_integer_) ]

Note that we must use NA_integer_ and not NA if we want it to not only
work in the case where some rows have no 01 but also work in the case
that there are no 01's in any row at all.