[R] the hat ^ in regular expression
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Feb 8 14:00:37 CET 2010
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