0

I have a dataframe with 0 and 1 values like this:

stock1,stock2,stock3,stock4
1,0,0,0
1,0,1,0
0,0,1,0
1,0,1,0

I would like to remove the rows which all their rows have 0 values, in the previous example they are stoc2 and stock4 columns. In order to make it I calculate the sum and have a need row

stock1,stock2,stock3,stock4
1,0,0,0
1,0,1,0
0,0,1,0
1,0,1,0
3,0,3,0

Using something like this:

for (i in 1:ncol(df)){
      col_number <- paste("df$stock",i)
      col_number <- NULL # I could find a way to have the df column and not the variable
}

It is a little frustrated way. Any idea if there is a more optimal way to make it?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Keri
  • 375
  • 1
  • 3
  • 14

1 Answers1

1

The description of the question and the example seems to be inconsistent. If we need to remove the rows, where there are only 0's, then we can use rowSums

df1[rowSums(df1 != 0)>0,]

If we need to remove the columns that have all values 0, then, do

Filter(sum, df1)

Or to make it explicit and to work on edge cases

Filter(function(x) any(x!=0), df1)
#   stock1 stock3
#1      1      0
#2      1      1
#3      0      1
#4      1      1
akrun
  • 874,273
  • 37
  • 540
  • 662