Aviad Klein
2011-Feb-04 15:46 UTC
[R] <environment: 0x000000000734f888> in object of class formula
# 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]]
Prof Brian Ripley
2011-Feb-04 16:18 UTC
[R] <environment: 0x000000000734f888> in object of class formula
Did you look at the help? ?formula has a whole section about why formulas have environments and what they do. On Fri, 4 Feb 2011, Aviad Klein wrote:> # 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?See also ?environment for how to remove (sic) it.> > # thank you, > > # Aviad > # aviadklein.wordpress.com/ > > [[alternative HTML version deleted]]Please don't send HTML when expressly asked not to in the posting guide. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595