[R] the hat ^ in regular expression
kMan
kchamberln at gmail.com
Wed Feb 10 04:37:21 CET 2010
Use "\\^" instead.
For example:
df<-data.frame(c(1,2,4),c(4,3,2))
names(df)<-c("Amt","Resp")
df.form<-formula(Resp~0+Amt+I(Amt^2), data=df)
strsplit(as.character(df.form)[3], "\\^", perl=T)
should work just fine.
Sincerely,
KeithC.
-----Original Message-----
From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com]
Sent: Monday, February 08, 2010 6:01 AM
To: christophe dutang
Cc: r-help at r-project.org
Subject: Re: [R] the hat ^ in regular expression
Try this:
> library(gsubfn)
> myexpr <- expression( ( mydata$variable1 / mydata$variable2 ) ^ 2 - 1
> + 3 * 4) strapply(as.character(myexpr), "mydata\\$(\\w+)")[[1]]
[1] "variable1" "variable2"
See http://gsubfn.googlecode.com for more info on strapply.
Another approach is
> setdiff(all.vars(myexpr), "mydata")
[1] "variable1" "variable2"
On Mon, Feb 8, 2010 at 6:22 AM, christophe dutang <dutangc at gmail.com> wrote:
> Dear UseRs,
>
>
> I'm trying to find variable names (string after the "mydata$") in a
> expression. For example,
>
> myexpr <- expression( ( mydata$variable1 / mydata$variable2 ) ^ 2 - 1
> + 3 *
> 4 )
>
> I would like to get "variable1" and "variable2". The following few
> lines split the original character string into pieces.
>
> mystring <- as.character(myexpr)
>
> mydatapositions <- gregexpr("mydata", as.character(myexpr))[[1]]
>
> mydataend <- mydatapositions + attr(mydatapositions, "match.length")
> +1
>
> mydatabegin <- c(mydatapositions, nchar(mystring))
>
> In this loop, I try to remove operator signs, spaces and brackets. But
> I could not match the hat ^ in the string.
>
> for(i in 1:length(mydatapositions ))
> {
>
> nomydata <- substr(mystring, mydataend[i], mydatabegin[i+1]-1)
> nomydata <- gsub("\ ","",nomydata)
> print(nomydata)
>
> cat("_____\n")
> res <- gregexpr("[+]",nomydata)
> print(c(gsub("[+]","",nomydata), unlist(res)))
>
> res <- gregexpr("[-]",nomydata)
> print(c(gsub("[-]","",nomydata), unlist(res)))
>
> res <- gregexpr("[/]",nomydata)
> print(c(gsub("[/]","",nomydata), unlist(res)))
>
> res <- gregexpr("[/]",nomydata)
> print(c(gsub("[*]","",nomydata), unlist(res)))
>
> res <- gregexpr(")",nomydata)
> print(c(gsub(")","",nomydata), unlist(res)))
>
> res <- gregexpr("\^",nomydata)
> print(c(gsub("\^","",nomydata), unlist(res)))
>
> print(gsub("[0-9]","",nomydata))
>
> cat("-------------\n")
> }
>
> I get the following warnings telling me the character is not
> recognized but I don't know how to solve the problem...
>
> Warning messages:
> 1: '\^' is an unrecognized escape in a character string
> 2: unrecognized escape removed from "\^"
> 3: '\^' is an unrecognized escape in a character string
> 4: unrecognized escape removed from "\^"
>
> Any help is welcome.
>
> Christophe
>
> --
> Christophe DUTANG
> Ph. D. student at ISFA
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list