simplelenz
simplelenz

Reputation: 947

Remove 0 columns from a data frame in R

I see this question has been asked before but the solutions provided by them gives some strange results in my case.

my data frame(df) is this

         Department1  Department2  Department3  Cafeteria  Lobby(TT)  Lobby(Music Band)
  James  0            1            0            0          0          0
  Flynn  0            1            0            0          0          0
  Liam   0            1            0            0          0          0

My desired result is

       Department2  
James  1           
Flynn  1           
Liam   1           

My code used to remove zero columns is

df <- df[, colSums(df != 0) > 0]

Above code was taken by this

https://stackoverflow.com/a/21530306/7857035

The result get is

1  1
2  1
3  1

Above code works when there are more than one column which contains different values other than zeros. How to get the desired result in this case?

Upvotes: 0

Views: 287

Answers (2)

Kevin Arseneau
Kevin Arseneau

Reputation: 6264

I'm getting accustomed to using purrr::keep for similar tasks to this.

library(tibble)
library(dplyr)
library(purrr)

df <- read.table(text = "
name   Department1  Department2  Department3  Cafeteria  lobby_TT   lobby_music
James  0            1            0            0          0          0
Flynn  0            1            0            0          0          0
Liam   0            1            0            0          0          0",
                 header = TRUE, stringsAsFactor = FALSE)

df %>%
  column_to_rownames("name") %>%
  keep(~all(.x) != 0)

#       Department2
# James           1
# Flynn           1
# Liam            1

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522762

The immediate fix to your problem is to use drop=FALSE when subsetting the data frame:

df <- df[, colSums(df != 0) > 0, drop=FALSE]

This will tell R not to coerce the data frame down to the lowest dimension, which in this case is a numeric vector. As you seem to have already noticed, coercion would not be a problem if you have more than one non zero sum column.

Demo

Upvotes: 2

Related Questions