[R] merging columns

Chuck White chuckwhite8 at charter.net
Wed Feb 3 03:15:26 CET 2010

Hello -- I am trying to merge columns in a dataframe based on substring matches in colnames. I would appreciate if somebody can suggest a faster/cleaner approach (eg. I would have really liked to avoid the if-else piece but rowSums does not like that). Thanks.

data.df <- data.frame(aa=c(1,1,0), bbcc=c(1,0,0), aab=c(0,1,0), aac=c(0,0,1), bbk=c(1,0,1))
col2 <- substr(colnames(data.df),1,2)

col2.uniq <- unique(col2)
names(col2.uniq) <- col2.uniq

data.frame(sapply(col2.uniq, function(col) {
  wcol <- which(col==col2)
  if(length(wcol)>1) {
    tmp <- rowSums(data.df[,wcol])
  } else {
    tmp <- data.df[,wcol]

More information about the R-help mailing list