[R] Assigning variable value as name to cbind column
Ralf B
ralf.bierig at gmail.com
Fri Jun 25 07:47:48 CEST 2010
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
More information about the R-help
mailing list