Skip to content

grep pattern

7 messages · Kang Min, David Winsemius, jim holtman

#
Hi all,

I'm trying to subset a pattern in a vector. Each argument has 6
letters, and I need those that start with Z and end with Z.

e.g.
 x <- c("ZFHSJK", "ZFHJKZ","ZIOPWE","ZLKJSD","ZKFLPZ")

I've looked up other discussions but still can't seem to find the
answer.

Thanks.
Kangmin
#
On May 20, 2011, at 11:57 AM, Kang Min wrote:

            
You may need to study the regex page a bit longer

the "^" is the beginning of a string
".+" will math can arbitrarily long string of anything
and "$" indicates the end of a string

 > x <- c("ZFHSJK", "ZFHJKZ","ZIOPWE","ZLKJSD","ZKFLPZ")
 > grep("^Z.+Z$", x)
[1] 2 5
 > grep("^Z.+Z$", x, value=TRUE)
[1] "ZFHJKZ" "ZKFLPZ"
David Winsemius, MD
West Hartford, CT
1 day later
#
Thanks!
On May 21, 7:09?am, David Winsemius <dwinsem... at comcast.net> wrote:
#
If you want to only match names of length 6, you will have to use this pattern:
+     "ZAAAAZAZ", "ZRITEZ")
[1] 2 5 9

        
On Sun, May 22, 2011 at 5:10 PM, Kang Min <ngokangmin at gmail.com> wrote:

  
    
1 day later
#
I have another question -

I'd like to extract dates from a vector of yyyy-mm-dd, so I just want
the dd.

x <- round(runif(10)*100000, digits=0)
y <- as.Date(x, origin="1970-01-01")

I tried this based on the code that Jim provided, but it just printed
the whole date. I think I just need to tweak it a little, but haven't
been able to figure it out.

y[grep("[[:digit:]]{2}$", y)]

Thanks.
Kang Min
On May 23, 7:22?am, jim holtman <jholt... at gmail.com> wrote:
#
On May 24, 2011, at 10:19 AM, Kang Min wrote:

            
Dates are stored as integers. You need to educate yourself about the  
differences between the internal and printed representations of R  
objects, starting with Dates and datatimes. but also including factors.

You also need to familiarize yourself with coercion functions such as  
as.character and as.numeric.
One of these would be more in keeping with what you asked for above
 > substr(as.character(y), 9,10)
  [1] "27" "24" "11" "14" "17" "05" "07" "16" "02" "12"
 > format(y, "%d"
+ )
  [1] "27" "24" "11" "14" "17" "05" "07" "16" "02" "12"


And please don't persist in appending questions to this thread. It's  
no longer about grep.
1 day later
#
try this using strsplit:
Class 'Date'  num [1:10] 26551 37212 57285 90821 20168 ...
chr [1:10] "2042-09-11" "2071-11-19" "2126-11-04" "2218-08-30"
"2025-03-21" "2215-12-22" ...
[[1]]
[1] "2042" "09"   "11"

[[2]]
[1] "2071" "11"   "19"

[[3]]
[1] "2126" "11"   "04"
chr [1:10] "11" "19" "04" "30" "21" "22" "24" "03" "31" "02"

        
On Tue, May 24, 2011 at 10:19 AM, Kang Min <ngokangmin at gmail.com> wrote: