Remove columns with zero values from a dataframe
I have a data.frame:
SelectVar
a b c d e f g h i j k l ll m n o p q r
1 Dxa8 Dxa8 0 Dxa8 Dxa8 0 Dxa8 Dxa8 0 0 0 0 0 0 0 0 0 Dxc8 0
2 Dxb8 Dxc8 0 Dxe8 Dxi8 0 tneg tpos 0 0 0 0 0 0 0 0 0 Dxi8 0
I would like to remove the columns with zero values in both rows from the data frame, so it yields a data frame as below:
SelectVar
a b d e g h q
1 Dxa8 Dxa8 Dxa8 Dxa8 Dxa8 Dxa8 Dxc8
2 Dxb8 Dxc8 Dxe8 Dxi8 tneg tpos Dxi8
Have tried:
SelectVar!=0
which yields a True/False dataframe, and:
SelectVar[, colSums(abs(SelectVar)) ! == 0]
which yields an error.
How could I remove the columns with zero values in each row?
You almost have it. Put those two together:
SelectVar[, colSums(SelectVar != 0) > 0]
This works because the factor columns are evaluated as numerics that are >= 1.
A dplyr friendly solution:
SelectVar %>% select_if(colSums(.) != 0)