# Hi all, # I've made a function to make a formula out of a data.frame without columns which contain a constant value.
# The function "which.constant" returns the indices of colums with constant values: which.constant <- function(data.frame) { # counts the number of columns in a data.frame which are constant h <- sapply(sapply(data.frame,unique),length) # count unique values return(as.numeric(which(h==1)))} # The function "make.formula" returns the desired formula but with an unwanted addition : make.formula <- function(data.frame) { h <- which.constant(data.frame) hh <- names(data.frame)[-h] hh <- paste("~",paste(hh,collapse="+"),sep="") return(as.formula(hh))} # The following structure should give an example of how it works: Data <- structure(list(cs_jail_2 = c(2L, 1L, 1L, 0L, 0L, 1L), cs_jail_3 = c(0L, 0L, 0L, 0L, 0L, 0L), cs_jail_4 = c(1L, 2L, 2L, 2L, 0L, 0L)), .Names = c("cs_jail_2", "cs_jail_3", "cs_jail_4"), row.names = c(NA, 6L), class = "data.frame") make.formula(Data) # ~cs_jail_2 + cs_jail_4 # <environment: 0x0000000007654058> # what does this <environment ...> mean? # does it effect computation in any way? how to get read of it? # thank you, # Aviad # aviadklein.wordpress.com/ [[alternative HTML version deleted]] ______________________________________________ R-help@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.