Z.Chanell
Z.Chanell

Reputation: 35

How ro remove rows where values in column A are smaller than values in column B

I am working in R and I am trying to make an if statement based on two columns, what I would like to do is to remove every row where the value in column A is smaller than the value in column B. How can I do this? The data:

           Stock   Minimum Stock 
Product A  35      32  
Product B  43      21  
Product C  12      15  
Product D  5       6 

I would like:

           Stock   Minimum Stock 
Product C  12      15  
Product D  5       6

Upvotes: 0

Views: 110

Answers (2)

Bogdan
Bogdan

Reputation: 161

If you want, you can take the above answer, or maybe if you are more of a beginner you can work with a for loop (and then maybe evolve to apply).

for(i in 1:nrow(df))
{
ifelse(df[i,1] < df[i,2], df <- df[-i,], next)
}

Good luck

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521457

It looks to me like you want the opposite, namely you want to retain rows where the stock is less than the minimum stock:

df[df$Stock < df$MinStock, ]

or

subset(df, Stock < MinStock)

Upvotes: 1

Related Questions