0

I need to change multiple values into NA or a string value based on 3 conditions. If the value in that column is smallen than that in 0, then turn it into NA.

If that value is greater or equal to the value in column x, then change to "known".

Otherwise change to "unknown".

Right now i do this in 3 lines of code, but they are overwriting each other (because line 2 is executed after line 1 which turn other values into NA.)


Lets say i have a data set x:

a1 a2 a3 a4 a5 a6

Where a1 to a6 have 200 rows containing all kind of numeric values then i want to change the values in a6 to NA whenever they are <= 0.

I want to change every value in a6 that is bigger than the value in a3 to "known".

And every other value in a6 must be set to "uknown".

I hope this clarifies a lot!

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Koen Wijnen
  • 55
  • 1
  • 8
  • Can you give sample data for illustrative purposes and your current code? I'm not quite sure what you're saying. – Daniel V Nov 13 '19 at 21:36
  • I have posted an answer on my question below, couldn't quickly find out how to create a sample data set here but i hope this answers clarifies – Koen Wijnen Nov 13 '19 at 21:40
  • 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. Edit your question to include more information. Do not post additional information as an "answer" below. – MrFlick Nov 13 '19 at 21:44

1 Answers1

1

Simple enough

a6 <- ifelse(a6 > 0, 
             ifelse(a6 > a3, "known", "unknown"),
             NA)
Daniel V
  • 1,305
  • 7
  • 23