0

I have data with various columns and I need to use few conditional statements on the columns. I am able to write following code successfully for conditions to be applied on two columns i.e., AB and MN.

if ("ABq" %in% colnames(x)) {
  x[ABq != 0, AB := NA]
  x[AB < 10 | DIF > 100, AB := NA]
}

if ("MNq" %in% colnames(x)) {
  x[MNq != 0, MN := NA]
  x[MN < 25 | MN > 150, MN := NA]
}

I have another column i.e., XY. I need to put a condition on XY that XY should have valid values only for the rows which have valid values for AB as well as MN, else it should be NA. (valid values mean with in the defined range). How can I write it in R?

  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 26 '23 at 13:39
  • Seems like you've already done the range tests on `MN` and `XY`, so you only need to look at whether those are `NA` or not. Maybe something like `x[, XY := fifelse(!is.na(AB) & !is.na(MN), XY, NA)]`. Or perhaps `x[is.na(AB) | is.na(MN), XY := NA]`. If I'm misunderstanding, please update the question with sample input and the corresponding desired output. – Gregor Thomas Jun 26 '23 at 13:41
  • @GregorThomas Thank you for your response. You understood it correctly. Could you please explain what is the difference between two statements you suggested? – Alexia k Boston Jun 30 '23 at 09:01
  • The two statements are different ways to do the same thing. The second way might be tiny bit faster. – Gregor Thomas Jun 30 '23 at 13:24

0 Answers0