Filter Rows Based on Multiple Column R Conditions

Suppose I have a dataset that contains 100 odd columns, and I only need to store those rows in the data that satisfy one condition that applies to all 100 columns. How to do it?

Suppose it looks like below ... I only need to store rows if any of Col1 or 2 or 3 or 4 columns is> 0

Col1 Col2 Col3 Col4 1 1 3 4 0 0 4 2 4 3 4 3 2 1 0 2 1 2 0 3 0 0 0 0 

In the above example, with the exception of the last line, all lines will do this. I need to place the results in the same data file as the original. not sure if I can use lapply to scroll columns where> 0, or I can use a subset. Any help is appreciated

Can I use column indices and do df<-subset(df,c(2:100)>0) . This does not give me the correct result.

+6
source share
2 answers

Suppose your file is data.frame DF , and then [ does your job.

 > DF[DF[,1]>0 | DF[,2] >0 | DF[,3] >0 | DF[,4] >0, ] Col1 Col2 Col3 Col4 1 1 1 3 4 2 0 0 4 2 3 4 3 4 3 4 2 1 0 2 5 1 2 0 3 

If you have hundreds of columns, you can use this alternative approach.

 > DF[rowSums(DF)=!0, ] Col1 Col2 Col3 Col4 1 1 1 3 4 2 0 0 4 2 3 4 3 4 3 4 2 1 0 2 5 1 2 0 3 
+11
source
 dat <- read.table(header = TRUE, text = " Col1 Col2 Col3 Col4 1 1 3 4 0 0 4 2 4 3 4 3 2 1 0 2 1 2 0 3 0 0 0 0 ") 

You can use data.table to automatically place as many columns as your data.frame. Here is one way, but perhaps a more elegant way to do it with data.table:

 require(data.table) dt <- data.table(dat) dt[rowSums(dt>0)>0] # Col1 Col2 Col3 Col4 # 1: 1 1 3 4 # 2: 0 0 4 2 # 3: 4 3 4 3 # 4: 2 1 0 2 # 5: 1 2 0 3 
+2
source

Source: https://habr.com/ru/post/953045/


All Articles