Re: [R] Assigning variable value as name to cbind column

From: <Bill.Venables_at_csiro.au>
Date: Fri, 25 Jun 2010 16:43:37 +1000

Why does the naming have to be done inside the cbind()?

How about

> dataTest <- data.frame(col1 = c(1,2,3))
> new.data <- c(1,2)
> name <- "test"

> length(new.data) <- nrow(dataTest)
> newDataTest <- cbind(dataTest, new.data)
> names(newDataTest)[[ncol(newDataTest)]] <- name
> newDataTest

  col1 test
1 1 1
2 2 2
3 3 NA

?

-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of Ralf B Sent: Friday, 25 June 2010 3:48 PM
To: r-help_at_r-project.org
Subject: [R] Assigning variable value as name to cbind column

Hi all,

I have this (non-working) script:

dataTest <- data.frame(col1=c(1,2,3))
new.data <- c(1,2)
name <- "test"
n.row <- dim(dataTest)[1]
length(new.data) <- n.row

names(new.data) <- name
cbind(dataTest, name=new.data)
print(dataTest)

and would like to bind the new column 'new.data' to 'dataTest' by using the value of the variable 'name' as the column name.

The end result should look like this:

      col1     test
1      1          1
2      2          2
3      3          NA


The best I got was that 'name' became the column name but never the actual value of 'name'. How can i do that?

(This is actually a function that runs many time -- this means a manual workaround is not feasible).

Ralf



R-help_at_r-project.org mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 25 Jun 2010 - 06:46:51 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 25 Jun 2010 - 06:50:35 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive