I like to combine some columns of a data.frame to a new column, like
dat <- data.frame(
color = sample(c("r","y","b"), 10, replace = TRUE),
year = sample(2011:2014, 10, replace = TRUE),
type = sample(c("early","mid","late"), 10, replace = TRUE))
dat$tot1 <- paste(dat$color, dat$year, dat$type)
that works, but how I do it on the basis of column names?
cnames <- c("color","year","type")
dat$tot2 <- do.call(paste, list(cnames,collapse=""))
of course, this only provides a column with "coloryeartype" entries and not the same as dat$tot1
. How would you do it? do.call(paste, list(get(cnames),collapse=""))
reports the error Error in get(cnames) : object 'color' not found
thx Christof
Just use
If you need
different
delimiter