[R] Dataframes and assign

Göran Broström gb at stat.umu.se
Wed Oct 2 15:52:50 CEST 2002


I have the following function:

putsoc <- function(age, dat){
  ## age is an integer
  ## dat is a data.frame

  new.name <- paste("soc", as.character(age), sep = ".")
  if (new.name %in% names(dat)) stop(paste(new.name, "already in place"))

  dat$new.var <- rep(1, nrow(dat)) ## Just nonsense.

  n.var <- ncol(dat)
  names(dat)[ncol(dat)] <- new.name
  return(dat)
}

i.e., the function adds a new variable to a data frame. The problem is
that I only have the new variable name in a text string, and so would
like to use 'assign'. I have two questions:

1. Is my solution 'safe': Can I trust that the new variable always will be
   the last one in the augmented data frame?

2. If I instead used 'assign(new.name, new.var)' how do I put the result
   into the data frame?

---
 Göran Broström                    tel: +46 90 786 5223
 Department of Statistics          fax: +46 90 786 6614
 Umeå University                   http://www.stat.umu.se/egna/gb/
 SE-90187 Umeå, Sweden             e-mail: gb at stat.umu.se


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list