# [R] Data manipulation in a data.frame

arun smartpink111 at yahoo.com
Sat Feb 22 01:51:44 CET 2014

```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 <ii54250 at msn.com> wrote:
Hello Arun,

Actually I do have rows with multiple 1s. Could you advise how to modify the
code then?