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
Bill.Venables at csiro.au
2010-Jun-25 06:43 UTC
[R] Assigning variable value as name to cbind column
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 > newDataTestcol1 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.
Henrique Dallazuanna
2010-Jun-25 12:37 UTC
[R] Assigning variable value as name to cbind column
Try this also: cbind(dataTest, `colnames<-`(cbind(new.data[1:nrow(dataTest)]), name)) On Fri, Jun 25, 2010 at 2:47 AM, Ralf B <ralf.bierig@gmail.com> wrote:> 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@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]