0

I want to collapse some variables into a single variable if the variable value is True

Eg: Data set

  • A B C D E F
  • F T F F...
  • F F T F..
  • F F F T..
  • T F F F..
  • F F F F....

The output I am looking for

  1. New_variable
    • B
    • C
    • D
    • A
    • .

As you can see if the row value is true I would like to replace it with its corresponding column value.

Thank you,

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Kp_Data
  • 13
  • 5

2 Answers2

1

We can use max.col to find the column name in each row based on the TRUE value

i1 <- rowSums(df1) == 0
df1$New_variable <- names(df1)[max.col(df1, "first")]
df1$New_variable[i1] <- ""
akrun
  • 874,273
  • 37
  • 540
  • 662
1

use any() to identify at least one value in the column is TRUE

df1 <- read.table(text='A B C D E F
           F T F F F F
                  F F T F F F
                  F F F T F F
                  T F F F F F
                  F F F F F F', header = TRUE)

colnames(df1)[ (apply( df1, 2, any)) ]
# [1] "A" "B" "C" "D"
Sathish
  • 12,453
  • 3
  • 41
  • 59