jessi
jessi

Reputation: 1518

For each row in data frame, return variable with non-zero column names

I am trying to create a variable that contains a list of all of the column names that are not zero for each row.

Example of data:

set.seed(334)
DF <- matrix(sample(0:9,9),ncol=4,nrow=10)
DF <- as.data.frame.matrix(DF)
DF$id <- c("ty18","se78","first", "gh89", "sil12","seve","aga2", "second","anotherX", "CH560")
DF$count <- rowSums(DF[,2:5]>0)
DF
>      V1 V2 V3 V4       id count
>   1   9  4  0  5     ty18     3
>   2   4  0  5  8     se78     3
>   3   0  5  8  2    first     4
>   4   5  8  2  6     gh89     4
>   5   8  2  6  7    sil12     4
>   6   2  6  7  3     seve     4
>   7   6  7  3  9     aga2     4
>   8   7  3  9  4   second     4
>   9   3  9  4  0 anotherX     3
>   10  9  4  0  5    CH560     3

The desired output would be a new variable that was, for row 1, "V1 V2 V4" and for row 2 "V1 V3 V4". I only want to use the V1-V4 for this, and not consider id or count.

This question on SO helped: For each row return the column name of the largest value

I tried to test this out, but it ignores my selective columns, even for max, so the first test here just gives the max for the whole row, which is not always in V1-V4 in my data.

DF$max <- colnames(DF)[apply(DF[,1:4],1,which.max)]

Despite the error, I think I need to do something like this, but my DF$list attempt is clearly all wrong:

DF$list <- colnames(DF[,1:4]>0)

I'm getting

Error in `$<-.data.frame`(`*tmp*`, "list", value = c("V1", "V2", "V3",  : 
replacement has 4 rows, data has 10

Maybe I'm trying to put a vector into a cell, and that is why it doesn't work, but I don't know how to get this information out and then make it into a string. I also don't understand why the max on selective columns did not work.

Upvotes: 1

Views: 3387

Answers (1)

orizon
orizon

Reputation: 3239

How about this

DF$nonzeros <- simplify2array(
                      apply(
                        DF[1:4], 1, 
                        function(x) paste(names(DF[1:4])[x != 0], collapse = " ")
                      )
                )

Upvotes: 5

Related Questions