Leo Del Mar
Leo Del Mar

Reputation: 37

Remove columns with more than x negative values

I have a Matrix with several negative values. I want to remove the columns containing more than 3 or 4 negative values, so that the ones remaining have 1 or 2 negative values only.

I cannot find an answer to my question. I know how to remove all columns containing at least 1 neg value, but not the selection I want to make (delete columns with 3+ negative values)

Dataframe is:

M = ( 1  0  0  1 -9  0  7  1)
    ( 1 -1  0  2  1  0  0  1)
    ( 1 -1 -5 -3 -7 -3 -5 -3)
    ( 4 -3  4 -2  3  4 -8  3)
    (-2  3  4 -3 -1 -4 -6 -2)

I need it to be (if 3 or more negative values):

M = ( 1  0  0  1)
    ( 1  0  0  1)
    ( 1 -5 -3 -3)
    ( 4  4  4  3)
    (-2  4 -4 -2)

Thanks a lot

Upvotes: 1

Views: 760

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388982

Here are few ways to do it

n <- 3
library(dplyr)
df %>% select_if(~sum(. < 0) < n)

#  V1 V3 V6 V8
#1  1  0  0  1
#2  1  0  0  1
#3  1 -5 -3 -3
#4  4  4  4  3
#5 -2  4 -4 -2

We can apply the same logic in apply/sapply

df[apply(df < 0 , 2, sum) < n]
df[sapply(df, function(x) sum(x < 0)) < n]

Another option is to count the sign of the numbers to filter columns

df %>% select_if(~sum(sign(.) == -1) < n)

df[apply(sign(df) == -1, 2, sum) < n]

df[sapply(sign(df), function(x) sum(x == -1)) < n]

Upvotes: 1

BENY
BENY

Reputation: 323226

Using colSums

x=2
df = df[,colSums(df<0)<=x]

Upvotes: 3

Related Questions