-3

I have a dataframe (data.table) I want to remove all columns where all values are equal to zero. I have read "Remove columns from dataframe where ALL values are NA" but doesn't help me much. My dataset has multiple columns over 3000. This reproducible is data.frame but how to tackle the same for data.table

Aquarius
  • 262
  • 1
  • 6
  • 20

1 Answers1

1

You can try something like this if you want to get rid of all columns that have all NA's or Zeroes. You can modify the condition accordingly if you want NAs only or zeroes only:

df <- df[, sapply(df, function(x) !all(is.na(x) | x == 0))]
Gopala
  • 10,363
  • 7
  • 45
  • 77