Skip to content

Instantly share code, notes, and snippets.

@lcomm
Created March 31, 2016 02:22
Show Gist options
  • Save lcomm/c91a30170a4b0a84cb6efcd69b88ef03 to your computer and use it in GitHub Desktop.
Save lcomm/c91a30170a4b0a84cb6efcd69b88ef03 to your computer and use it in GitHub Desktop.
Quick example of how you could extract ID variable from LME formula
#Function to extract the cluster ID variable from a formula
get_id <- function(formula){
#Extract the thing between the | and the ) in the last part of formula
#This is the clustering variable
idvar <- gsub('.*[|](.*?)).*','\\1',formula[-c(1:2)])
#Remove white space
idvar <- gsub("^\\s+|\\s+$", "", idvar)
#Return
return(idvar)
}
#Example use
get_id(formula = y ~ (x + z | id) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment