La Machine Infernale
La Machine Infernale

Reputation: 569

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?

Upvotes: 0

Views: 2774

Answers (2)

cderv
cderv

Reputation: 6542

Using dplyr

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.frame(var1,var2,var3,var4,var5)

library(dplyr)
# keep only columns for which all line are different from 0
df %>% select_if(function(col) !all(col == 0))
#>   var1 var2 var4
#> 1   13   20    4
#> 2   39   19    6
#> 3   14   20    2
#> 4   35   13    3
#> 5   35   17    4

Upvotes: 1

akrun
akrun

Reputation: 887118

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)

Upvotes: 0

Related Questions