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]
  }
  as.numeric(tmp>0)
}))

______________________________________________
[email protected] 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.

Reply via email to