Skip to content

variable name of variable in dataframe

4 messages · Hotz, T., Brian Ripley, Peter Dalgaard +1 more

#
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)

HTH

Thomas


---

Thomas Hotz
Research Associate in Medical Statistics
University of Leicester
United Kingdom

Department of Epidemiology and Public Health
22-28 Princess Road West
Leicester
LE1 6TP
Tel +44 116 252-5410
Fax +44 116 252-5423

Division of Medicine for the Elderly
Department of Medicine
The Glenfield Hospital
Leicester
LE3 9QP
Tel +44 116 256-3643
Fax +44 116 232-2976
#
There are easier (and more readable) ways: the simplest is perhaps to use

names(toyframe) <- paste("lion", b, sep=".")

after creating the data frame.  Others are to use  something like

arg <- list(a=c(1,2))
names(args) <- paste("lion", b, sep=".")
toyframe <- do.call("data.frame", args)

or to make use of substitute().  (Those really are `programming on the 
langauge' rather than using R as a text processor.)
On Fri, 25 Jul 2003, Hotz, T. wrote:

            

  
    
#
"Hotz, T." <th50 at leicester.ac.uk> writes:
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...)
#
Sir Hotz, 
Sir Bengtsson, 
Sir Ripley and
Sir Dalgaard,

Thank you __very__ much for
your help.

Tobias