Skip to content

Instantly share code, notes, and snippets.

@bullfight
Created September 7, 2010 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bullfight/569253 to your computer and use it in GitHub Desktop.
Save bullfight/569253 to your computer and use it in GitHub Desktop.
extract.formula<-function(formu){
if (!is(formu,"formula"))
stop("X should be a valid formula of form \n y ~ x1 + x2 | conditional")
resp <- formu[[2]]
if( length(formu[[3]]) < 2 ){ formu <- formu }else{ formu <- formu[[3]] }
flattener <- function(f) {if (length(f)<3) return(f);
c(Recall(f[[2]]),Recall(f[[3]]))}
if (formu[[1]] == '|'){
condi <- formu[[3]]
vars <- flattener(formu[[2]])
}else if(formu[[1]] == '+' & length(formu) < 4){
condi <- NA
vars <- c(formu[[2]], formu[[3]])
}else if(length(formu[[3]]) < 2){
condi <- NA
vars <- formu[[3]]
}
list(resp=resp,condi=condi,vars=vars)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment