[R] grep pattern

Kang Min ngokangmin at gmail.com
Tue May 24 16:19:32 CEST 2011


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:
> If you want to only match names of length 6, you will have to use thispattern:
>
> > x <- c("ZFHSJK", "ZFHJKZ","ZIOPWE","ZLKJSD","ZKFLPZ", "ZAAAAAAZ", "ZAZ",
>
> +     "ZAAAAZAZ", "ZRITEZ")
>
>
>
>
>
>
>
>
>
> > # match exactly values of length 6
> > len6 <- "^Z[[:alpha:]]{4}Z$"
> >grep(len6, x)
> [1] 2 5 9
>
> On Sun, May 22, 2011 at 5:10 PM, Kang Min <ngokang... at gmail.com> wrote:
> > Thanks!
>
> > On May 21, 7:09 am, David Winsemius <dwinsem... at comcast.net> wrote:
> >> On May 20, 2011, at 11:57 AM, Kang Min wrote:
>
> >> > Hi all,
>
> >> > I'm trying to subset apatternin 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.
>
> >> 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"
>
> >> > Thanks.
> >> > Kangmin
>
> >> > ______________________________________________
> >> > R-h... at r-project.org mailing list
> >> >https://stat.ethz.ch/mailman/listinfo/r-help
> >> > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> >> > and provide commented, minimal, self-contained, reproducible code.
>
> >> David Winsemius, MD
> >> West Hartford, CT
>
> >> ______________________________________________
> >> R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
>
> > ______________________________________________
> > R-h... at r-project.org mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> --
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
>
> ______________________________________________
> R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list