rrgui

How can I return column title within data.frame only if it contains a value of interest?


I have a data.frame containing multiple columns made up of TRUE and FALSE logical answers, as so:

>` .    X1    X2    X3    X4    X5    X6    X7    X8
 [1,] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE
 [2,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
 [3,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [4,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [5,] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
 [6,] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
 [7,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
`

I am trying to pull out only the titles of the columns where TRUE exists. For example here, I would get X4, X6, X7 and X8 out, but all others excluded.

For context, TRUE indicates a cell is responding to stimuli at that particular time point. I only need to know if the cell responds at all (i.e. TRUE exists in the column at least once), not when the cell responds.

Thank you in advance for any help regarding this!


Solution

  • Try:

    colnames(M)[colSums(M) >= 1]
    

    The colSums will sum the TRUE values, which can then be compared against a value of 1 to extract the column names.

    Example:

    M <- matrix(FALSE, nrow = 4, ncol = 5, dimnames = list(NULL, paste0("X", 1:5)))
    M[cbind(c(1, 2, 3, 4), c(2, 2, 4, 5))] <- TRUE
    M
    #         X1    X2    X3    X4    X5
    # [1,] FALSE  TRUE FALSE FALSE FALSE
    # [2,] FALSE  TRUE FALSE FALSE FALSE
    # [3,] FALSE FALSE FALSE  TRUE FALSE
    # [4,] FALSE FALSE FALSE FALSE  TRUE
    colnames(M)[colSums(M) >= 1]
    # [1] "X2" "X4" "X5"
    

    This approach would work whether you are dealing with a matrix or a data.frame.