Hi All,
I have a character vector with naems of cities in the us. I need to extract
the number that appear after the word "New York", for example,
x<-c("P Los Angeles44AZ", "P New York722AZ", "K New
York20")
I want the results to be
722, 20
cab I use the grep function, if so how?
I appreciate your help, thanks,
--
View this message in context:
http://n4.nabble.com/using-grep-tp1571102p1571102.html
Sent from the R help mailing list archive at Nabble.com.
Try this:
gsub(".*York(\\d+).*", "\\1", grep("New York", x,
value = TRUE))
On Fri, Feb 26, 2010 at 3:27 PM, kayj <kjaja27 at yahoo.com>
wrote:>
> Hi All,
>
> I have a character vector with naems of cities in the us. I need to extract
> the number that appear after the word "New York", for example,
>
> x<-c("P Los Angeles44AZ", "P New York722AZ", "K
New York20")
>
> I want the results to be
>
> 722, 20
>
>
> cab I use the grep function, if so how?
> I appreciate your help, thanks,
>
> --
> View this message in context:
http://n4.nabble.com/using-grep-tp1571102p1571102.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.
>
--
Henrique Dallazuanna
Curitiba-Paran?-Brasil
25? 25' 40" S 49? 16' 22" O
Hi ,
I have tried
gsub(".*York(\\d+).*", "\\1", grep("New York", x,
value = TRUE))
and outputs
"P New York722AZ" "K New York20"
but that is not what i want, I want the output to be
722,20
--
View this message in context:
http://n4.nabble.com/using-grep-tp1571102p1571251.html
Sent from the R help mailing list archive at Nabble.com.
On Feb 26, 2010, at 3:02 PM, kayj wrote:> > Hi , > > I have tried > > gsub(".*York(\\d+).*", "\\1", grep("New York", x, value = TRUE)) > > and outputs > > "P New York722AZ" "K New York20"Strange: > x<-c("P Los Angeles44AZ", "P New York722AZ", "K New York20") > > gsub(".*York(\\d+).*", "\\1", grep("New York", x, value = TRUE)) [1] "722" "20"> but that is not what i want, I want the output to be > > 722,20 >Aside from being a character vector without commas, it seemed pretty close. David Winsemius, MD Heritage Laboratories West Hartford, CT
Look at the gsubfn package, it gives more options and will probably make what you are trying to do easier. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of kayj > Sent: Friday, February 26, 2010 11:27 AM > To: r-help at r-project.org > Subject: [R] using grep > > > Hi All, > > I have a character vector with naems of cities in the us. I need to > extract > the number that appear after the word "New York", for example, > > x<-c("P Los Angeles44AZ", "P New York722AZ", "K New York20") > > I want the results to be > > 722, 20 > > > cab I use the grep function, if so how? > I appreciate your help, thanks, > > -- > View this message in context: http://n4.nabble.com/using-grep- > tp1571102p1571102.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.
Here it is using strapply in gsubfn. x is the input, followed by the regular expression which is just New York followed by a parenthesized string of digits. The parenthesized portion is passed to the function, as.numeric, and then everything is simplified using c (otherwise we would get a list as in similar R core functions such as strsplit).> strapply(x, "New York(\\d+)", as.numeric, simplify = c)[1] 722 20 On Sat, Feb 27, 2010 at 12:25 PM, Greg Snow <Greg.Snow at imail.org> wrote:> Look at the gsubfn package, it gives more options and will probably make what you are trying to do easier. > > -- > Gregory (Greg) L. Snow Ph.D. > Statistical Data Center > Intermountain Healthcare > greg.snow at imail.org > 801.408.8111 > > >> -----Original Message----- >> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- >> project.org] On Behalf Of kayj >> Sent: Friday, February 26, 2010 11:27 AM >> To: r-help at r-project.org >> Subject: [R] using grep >> >> >> Hi All, >> >> I have a character vector with naems of cities in the us. I need to >> extract >> the number that appear after the word "New York", for example, >> >> x<-c("P Los Angeles44AZ", "P New York722AZ", "K New York20") >> >> I want the results to be >> >> 722, 20 >> >> >> cab I use the grep function, if so how? >> I appreciate your help, thanks, >> >> -- >> View this message in context: http://n4.nabble.com/using-grep- >> tp1571102p1571102.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> R-help at 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 at 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. >
Then use strsplit() instead.
KeithC.
-----Original Message-----
From: kayj [mailto:kjaja27 at yahoo.com]
Sent: Friday, February 26, 2010 1:02 PM
To: r-help at r-project.org
Subject: Re: [R] using grep
Hi ,
I have tried
gsub(".*York(\\d+).*", "\\1", grep("New York", x,
value = TRUE))
and outputs
"P New York722AZ" "K New York20"
but that is not what i want, I want the output to be
722,20
--
View this message in context:
http://n4.nabble.com/using-grep-tp1571102p1571251.html
Sent from the R help mailing list archive at Nabble.com.