> grep("f[0-9]+=", "f1=5,f22=3,", value = T)[1] "f1=5,f22=3," How do I make the line output c("f1", "f22") instead? (Actually, c(1,22) would be even better). Thank you. -- View this message in context: http://r.789695.n4.nabble.com/Quick-GREP-challenge-tp2339486p2339486.html Sent from the R help mailing list archive at Nabble.com.
It isn't entirely clear what you are trying to do - your grep() statement simply returns the entire string you started with. But to turn that string into a vector, you will need some combination of gsub(), strsplit(), and as.numeric() with the exact values depending on the exact form of the output returned by grep. By c(1, 22) do you mean a vector (my first assumption), or do you actually mean the string "c(1, 22)"? If the former, maybe something like this:> x <- "f1=5,f22=3" > x <- gsub("=[0-9]+", "", x) > x[1] "f1,f22"> x <- gsub("f", "", x) > x[1] "1,22"> x <- strsplit(x, ",") > x[[1]] [1] "1" "22"> x <- unlist(x) > x[1] "1" "22"> x <- as.numeric(x) > x[1] 1 22 Sarah On Thu, Aug 26, 2010 at 6:16 AM, Dimitri Shvorob <dimitri.shvorob at gmail.com> wrote:> >> grep("f[0-9]+=", "f1=5,f22=3,", value = T) > [1] "f1=5,f22=3," > > How do I make the line output c("f1", "f22") instead? (Actually, c(1,22) > would be even better). > > Thank you. > >-- Sarah Goslee http://www.functionaldiversity.org
On Thu, Aug 26, 2010 at 6:16 AM, Dimitri Shvorob <dimitri.shvorob at gmail.com> wrote:> >> grep("f[0-9]+=", "f1=5,f22=3,", value = T) > [1] "f1=5,f22=3," > > How do I make the line output c("f1", "f22") instead? (Actually, c(1,22) > would be even better). >strapply in gsubfn extracts matches based on content rather than delimiters. See home page for more info http://gsubfn.googlecode.com> library(gsubfn) > x <- "f1=5,f22=3," > strapply(x, "f\\d+")[[1]][1] "f1" "f22" -- GKX Group GKX Associates Inc. 1-877-GKX-GROUP ggrothendieck at gmail.com
On Aug 26, 2010, at 8:15 AM, Gabor Grothendieck wrote:> On Thu, Aug 26, 2010 at 6:16 AM, Dimitri Shvorob > <dimitri.shvorob at gmail.com> wrote: >> >>> grep("f[0-9]+=", "f1=5,f22=3,", value = T) >> [1] "f1=5,f22=3," >> >> How do I make the line output c("f1", "f22") instead? (Actually, >> c(1,22) >> would be even better). >> > > strapply in gsubfn extracts matches based on content rather than > delimiters. See home page for more info http://gsubfn.googlecode.com > >> library(gsubfn) >> x <- "f1=5,f22=3," >> strapply(x, "f\\d+")[[1]] > [1] "f1" "f22"I read the request as for c(1,22) offered without quotes so t emeaning was obscure but perhaps this is what OP wanted: as.numeric( strapply(x, "f(\\d+)=")[[1]] ) [1] 1 22 # or > as.numeric(unlist(strapply(x, "f(\\d+)="))) [1] 1 22> > -- > GKX Group > GKX Associates Inc. > 1-877-GKX-GROUP > ggrothendieck at gmail.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.David Winsemius, MD West Hartford, CT
Many thanks! -- View this message in context: http://r.789695.n4.nabble.com/Quick-GREP-challenge-tp2339486p2339818.html Sent from the R help mailing list archive at Nabble.com.
Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Dimitri Shvorob > Sent: Thursday, August 26, 2010 3:16 AM > To: r-help at r-project.org > Subject: [R] Quick GREP challenge > > > > grep("f[0-9]+=", "f1=5,f22=3,", value = T) > [1] "f1=5,f22=3," > > How do I make the line output c("f1", "f22") instead? > (Actually, c(1,22) > would be even better).If you had S+ you could use subpattern and keep arguments to strsplit (which otherwise acts like R's strsplit): > strings <- c("f1=5,f22=3,", "g4,f55,f66", "hello") > strsplit(strings, "f([[:digit:]]+)", subpattern=1, keep=TRUE) [[1]]: [1] "1" "22" [[2]]: [1] "55" "66" [[3]]: character(0) keep=TRUE means to split the string by what is not in the pattern (keeping what is in the pattern) instead of the usual splitting by what is in the pattern and keeping what is not in the pattern. subpattern=n means to match by the whole pattern but to use only the n'th parenthesized subpattern when deciding what to split by or keep. In this case the whole pattern matches 'f' followed by 1 or more digits but we only want the keep the digits. (subpattern=0, the default, means to use the entire pattern.) You still need to use as.integer() or as.numeric() on the output elements. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> > > Thank you. > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Quick-GREP-challenge-tp2339486p2339486.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. >