r - How to test if a formula is one-sided? -


i need test if formula one-sided (e.g. ~ a rather a~b).

right i'm doing this:

test <- list( ~ + b, ~ b + c, b + c ~ ) isonesided <- function(form) length(form)==2 && sum(grepl("~",form))==1 > sapply(test,isonesided) [1]  true false false 

is there better way? i'm worried there types of formulae don't know elude test.

i use terms function , extract response attribute:

test <- list( ~ + b, ~ b + c, b + c ~ ) sapply( test , function(x) attr( terms(x) , "response" ) == 0 ) # [1]  true false false 

edit

as @arun points out terms can't expand formula object special . in without knowing data.frame special refers to. workaround include dummy data.frame in terms function call:

## if want expand '.' in b + c ~ .     test <- list( ~ + b, ~ b + c, b + c ~ ,  b + c ~ . , . ~ b + c  ) sapply( test , function(x) attr( terms(x , data = data.frame(runif(1))) , "response" ) == 0 ) # [1]  true false false false false 

Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -