[R] Column name defined by function variable

Ralf B ralf.bierig at gmail.com
Thu Jun 17 11:09:34 CEST 2010


Hi all,

probably a simple problem for you but I am stuck.

This simple function adds columns (with differing length) to data frames:

add.col <- function(df, new.col) {
	n.row <- dim(df)[1]
	length(new.col) <- n.row
	cbind(df, new.col)
}

Now I would like to extend that method. A new parameter 'name' shouild
allow people to pass in a name for that new column. Is that possible
and how can this be achieved?

Example:

myData <- data.frame(c(1,2,3))
add.col(myData, c(5,6,7,8), 'more stuff')

adds a new column named 'more stuff' to the dataframe myData.


Any ideas?

Best,
Ralf



More information about the R-help mailing list