Kevin Wang
Kevin Wang

Reputation: 1

Removing columns that are all 0

I am trying to remove all columns in my dataframe that solely contain the value 0. My code is the following that I found on this website.

dataset = dataset[ ,colSums(dataset != 0) > 0]

However, I keep returning an error:

Error in [.data.frame(dataset, , colSums(dataset != 0) > 0) :
undefined columns selected

Upvotes: 0

Views: 197

Answers (3)

Uwe
Uwe

Reputation: 42544

There is an alternative using all():

dataset[, !sapply(dataset, function(x) all(x == 0))]
  a  c  d f
1 1 -1 -1 a
2 2  0 NA a
3 3  1  1 a

In case of a large dataset, time and memory consuming copying can be avoided through removing the columns by reference

library(data.table)
cols <- which(sapply(dataset, function(x) all(x == 0)))
setDT(dataset)[, (cols) := NULL]
dataset

   a  c  d f
1: 1 -1 -1 a
2: 2  0 NA a
3: 3  1  1 a

Data

dataset <- data.frame(a = 1:3, b = 0, c = -1:1, d = c(-1, NA, 1), e = 0, f ="a")
dataset
  a b  c  d e f
1 1 0 -1 -1 0 a
2 2 0  0 NA 0 a
3 3 0  1  1 0 a

Upvotes: 0

DanY
DanY

Reputation: 6073

Here's some code that will check which columns are numeric (or integer) and drop those that contain all zeros and NAs:

# example data
df <- data.frame( 
        one = rep(0,100), 
        two = sample(letters, 100, T), 
        three = rep(0L,100), 
        four = 1:100,
        stringsAsFactors = F
      )

# create function that checks numeric columns for all zeros
only_zeros <- function(x) {
    if(class(x) %in% c("integer", "numeric")) {
        all(x == 0, na.rm = TRUE) 
    } else { 
        FALSE
    }
}

# apply that function to your data
df_without_zero_cols <- df[ , !sapply(df, only_zeros)]

Upvotes: 0

stevec
stevec

Reputation: 52268

It's because you have an NA in at least one column. Fix like this:

dataset = dataset[ , colSums(dataset != 0, na.rm = TRUE) > 0]

Upvotes: 3

Related Questions