From ggrothendieck at gmail.com  Fri Nov 11 16:31:50 2011
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Fri, 11 Nov 2011 10:31:50 -0500
Subject: [Rd] One step way to create data frame with variable "variable
	names"?
In-Reply-To: <CAErODj9qO+8AruqRjdLTGcWaNgR8ptP3TUv-Y6hOv20r1eTtUg@mail.gmail.com>
References: <CAErODj9qO+8AruqRjdLTGcWaNgR8ptP3TUv-Y6hOv20r1eTtUg@mail.gmail.com>
Message-ID: <CAP01uRkBab+e3__pdP+F+e8d3TJPi84OD_UvdSWD7e7aEJFFDA@mail.gmail.com>

On Fri, Nov 11, 2011 at 10:25 AM, Paul Johnson <pauljohn32 at gmail.com> wrote:
> Suppose
>
> plotx <- "someName"
> modx <- "otherName"
> plotxRange <- c(10,20)
> modxVals <- c(1,2,3)
>
> It often happens I want to create a dataframe or object with plotx or
> modx as the variable names. ?But can't understand syntax to do that.
>
> I can get this done in 2 steps, creating the data frame and then
> assigning names, as in
>
> newdf <- data.frame( c(1, 2, 3, 4), c(4, 4, 4, 4))
> colnames(newdf) <- c(plotx, modx)
>
> I was trying to hit this in one step, but can't find how. ?If I could
> get this in one step, it would simplify some book keeping, because
> this current method requires me to build up the name vector to match
> newdf, and sometimes I make mistakes.
>
> After the data.frame already exists, I understand I can insert new
> variables using the variable names with syntax like
>
> newdf[[plotx]] <- c(23, 23, 23, 23)
>


Try this:

newdf <- setNames(data.frame( c(1, 2, 3, 4), c(4, 4, 4, 4)), c(plotx, modx))

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



[Rd] One step way to create data frame with variable "variable names"?
Gabor Grothendieck