[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:
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:
# [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.
# [1] "D"    "B"    "B"    "B"    "C"    "C"    "D"    "none" "none" "B,C" 
#[11] "C"    "B,D"  "C,D" 


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?

Thanks in advance, 


More information about the R-help mailing list