[R] variable name of variable in dataframe

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Fri Jul 25 13:04:06 CEST 2003


"Hotz, T." <th50 at leicester.ac.uk> writes:

> Dear Tobias,
> 
> The trick is "Programming on the Language", see e.g. the "R Language Manual". 
> Construct the expression you want, and have it explicitly parsed and evaluated.
> 
> toy <- function(b=.95){
>   toyframe <- eval(parse(text=paste("data.frame(lion", b, " = c(1, 2))", sep="")))
>   return(toyframe)
> z}
> toy()
> toy(0)

A bit of an overkill in this case:

toy <- function(b=.95){
    toyframe <- data.frame(x=c(1,2))
    names(toyframe) <- paste("lion",b,sep="")
    toyframe
}

(Getting rid of the leading 0 is left as an exercise...)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list