La Machine Infernale La Machine Infernale - 1 month ago 10
R Question

R: remove columns based on row value

did some research but was unable to find a straight answer for my simple problem.
I have a data table like this:

var1<-sample(12:43,5,replace=T)
var2<-sample(5:20,5,replace=T)
var3<-rep(0, 5)
var4<-sample(2:6,5,replace=T)
var5<-rep(0,5)
df<-data.table(var1,var2,var3,var4,var5)

var1 var2 var3 var4 var5
1: 36 14 0 2 0
2: 43 19 0 6 0
3: 20 6 0 6 0
4: 41 17 0 2 0
5: 32 8 0 6 0


I want to remove all columns containing only 0 values.
This will generate a logical vector.

a<-df[,(df[20,]) != 0]


How can I use my logical vector to subset the data?

Answer

If we need the data.table methods, loop through the Subset of data.table (lapply(.SD), check whether all the values are 0, unlist the output, negate (!) and subset the columns based on the logical index.

 df[, df[, !unlist(lapply(.SD, function(x) all(x==0)))], with = FALSE]

Or using base R, we can Filter the columns where the var or sum is not 0.

 Filter(var, df)