dimanche 25 avril 2021

keeping the original values in a data frame based on a condition

Im trying to filter/change the value in a data frame based on a set of conditions.

 a <- c(18, 12, 15, 8, 4, 2, 9, 1)
 b <- 14

 c = a - b 
  [1]   4  -2   1  -6 -10 -12  -5 -13

Is it possible that where the output is negative, it takes the original number?

For example

18 - 14 = 4 (positive so its OK)

12 - 14 = -2 (negative so it should just stick with the 12)

What is the best way to do this?

Aucun commentaire:

Enregistrer un commentaire