Paul Johnson
2011-Nov-11 15:25 UTC
[Rd] One step way to create data frame with variable "variable names"?
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) PJ -- Paul E. Johnson Professor, Political Science 1541 Lilac Lane, Room 504 University of Kansas
Gabor Grothendieck
2011-Nov-11 15:31 UTC
[Rd] One step way to create data frame with variable "variable names"?
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
William Dunlap
2011-Nov-11 16:09 UTC
[Rd] One step way to create data frame with variable "variable names"?
> plotx <- "someName" > modx <- "otherName" > data.frame( structure(list(c(1, 2, 3, 4)), names=plotx), structure(list(c(4, 4, 4, 4)), names=modx))someName otherName 1 1 4 2 2 4 3 3 4 4 4 4 (I think this is more of an R-help question.) Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-project.org] On Behalf Of Paul Johnson > Sent: Friday, November 11, 2011 7:25 AM > To: R Devel List > Subject: [Rd] One step way to create data frame with variable "variable names"? > > 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) > > PJ > > -- > Paul E. Johnson > Professor, Political Science > 1541 Lilac Lane, Room 504 > University of Kansas > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Reasonably Related Threads
- How to save a model in DB and retrieve It
- Salvar los plots a alta resoluciĆ³n la vez de incluirlos en documento
- Ror has problems with memory usage??
- Unexpected behavior in factor level ordering
- Error in names(x) <- value: 'names' attribute must be the same length as the vector