Re: [R] string pattern matching

2017-03-23 Thread Joe Ceradini
Oh, I see the utility of that now. Definitely will be using "term.labels" (which I was not aware of). Thanks! On Thu, Mar 23, 2017 at 9:48 AM, William Dunlap wrote: > If you are trying to see if one model nested in another then I think > looking at the 'term.labels' attribute of terms(formula) i

Re: [R] string pattern matching

2017-03-23 Thread William Dunlap via R-help
If you are trying to see if one model nested in another then I think looking at the 'term.labels' attribute of terms(formula) is the way to go. Most formula-based modelling functions store the output of terms(formula) in their output and many supply a method for the terms function that extracts th

Re: [R] string pattern matching

2017-03-23 Thread Joe Ceradini
Thanks for the additional response, Bill. I did not want to bog down the question with the full context of the function. Briefly, given a set of nested and non-nested regression models, I want to compare AIC (bigger model - smaller model) and do an LRT for all the nested models that differ by a sin

Re: [R] string pattern matching

2017-03-22 Thread William Dunlap via R-help
You did not describe the goal of your pattern matching. Were you trying to match any string that could be interpreted as an R expression containing X1 and X3 as additive terms? If so, you could turn the string into a one-sided formula and use the terms() function. E.g., f <- function(string) {

Re: [R] string pattern matching

2017-03-22 Thread Joe Ceradini
Wow. Thanks to everyone (Jim, Ng Bo Lin, Bert, David, and Ulrik) for all the quick and helpful responses. They have given me a better understanding of regular expressions, and certainly answered my question. Joe On Wed, Mar 22, 2017 at 12:22 AM, Ulrik Stervbo wrote: > Hi Joe, > > you could also

Re: [R] string pattern matching

2017-03-21 Thread Ulrik Stervbo
Hi Joe, you could also rethink your pattern: grep("x1 \\+ x2", test, value = TRUE) grep("x1 \\+ x", test, value = TRUE) grep("x1 \\+ x[0-9]", test, value = TRUE) HTH Ulrik On Wed, 22 Mar 2017 at 02:10 Jim Lemon wrote: > Hi Joe, > This may help you: > > test <- c("x1", "x2", "x3", "x1 + x2 +

Re: [R] string pattern matching

2017-03-21 Thread David Winsemius
> On Mar 21, 2017, at 6:31 PM, Bert Gunter wrote: > > It is not clear to me what you mean, but: > >> grep ("x1 \\+.* \\+ x3",test, value = TRUE) > [1] "x1 + x2 + x3" > > ## This will miss "x1 + x3" though. So then this might be acceptable: grep ("x1\\ \\+.* x3", test, value = TRUE) > > se

Re: [R] string pattern matching

2017-03-21 Thread Bert Gunter
It is not clear to me what you mean, but: > grep ("x1 \\+.* \\+ x3",test, value = TRUE) [1] "x1 + x2 + x3" ## This will miss "x1 + x3" though. seems to do what you want, maybe. Perhaps you need to read up about regular expressions and/or clarify what you want to do. Cheers, Bert Bert Gunter "

Re: [R] string pattern matching

2017-03-21 Thread Jim Lemon
Hi Joe, This may help you: test <- c("x1", "x2", "x3", "x1 + x2 + x3") multigrep<-function(x1,x2) { xbits<-unlist(strsplit(x1," ")) nbits<-length(xbits) xans<-rep(FALSE,nbits) for(i in 1:nbits) if(length(grep(xbits[i],x2))) xans[i]<-TRUE return(all(xans)) } multigrep("x1 + x3","x1 + x2 + x3")

[R] string pattern matching

2017-03-21 Thread Joe Ceradini
Hi Folks, Is there a way to find "x1 + x2 + x3" given "x1 + x3" as the pattern? Or is that a ridiculous question, since I'm trying to find something based on a pattern that doesn't exist? test <- c("x1", "x2", "x3", "x1 + x2 + x3") test [1] "x1" "x2" "x3" "x1 + x2 +