Hi Ioanna, If you need to paste the colnames if there are multiple 1's per row: You could try: A<-data.frame(A=c(10,100,1000,30,50,60,300,3,4,2,20,35,45),B=c(0,1,1,1,0,0,0,0,0,1,0,0,1),C=c(0,0,0,0,1,1,0,0,0,0,1,1,1),D=c(1,0,0,0,0,0,1,0,0,1,NA,1,1)) apply(A[,-1],1,function(x) {x1 <-paste(colnames(A[,-1])[x & !is.na(x)],collapse=","); x1[x1=='']<- "none";x1}) #[1] "D" "B" "B" "B" "C" "C" "D" "none" "none" #[10] "B,D" "C" "C,D" "B,C,D"
#or Bert's method with some modification: c("none",names(A)[-1],"B,D","C,D","B,C,D")[c(as.matrix(!!A[,-1]&!is.na(A[,-1]))%*%seq_len(ncol(A)-1)+1)] # [1] "D" "B" "B" "B" "C" "C" "D" "none" "none" #[10] "B,D" "C" "C,D" "B,C,D" But, in this case, you may need to check if the combinations are there or not in the dataset, Otherwise For e.g. c("none",names(A)[-1],apply(combn(LETTERS[2:4],2),2,paste,collapse=","),"B,C,D")[c(as.matrix(!!A[,-1]&!is.na(A[,-1]))%*%seq_len(ncol(A)-1)+1)] # [1] "D" "B" "B" "B" "C" "C" "D" "none" "none" "B,C" #[11] "C" "B,D" "C,D" A.K. On Friday, February 21, 2014 4:20 PM, ioanna ioannou <ii54...@msn.com> wrote: Hello Arun, Actually I do have rows with multiple 1s. Could you advise how to modify the code then? Thanks in advance, Best IOanna ______________________________________________ R-help@r-project.org 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.