jeudi 10 novembre 2016

if then do multiple statements from SAS to R

I am new to R. Here is the SAS script that I want to replicate in R:

if a=4 or b=4 then do;
  if a=1 or b=1 then news="AB";
  else if a=2 or b=2 then news="BC";
  else news="CD";
  end;
else do;
  if a=1 or b=1 then news="DF";
  else news="GH";
end;

I cannot find a way of ifelse or other methods to do that in R. Can you help me? Thank you!

Here is my first try on first part of the above script:

news <- ifelse(a==4 | b==4,
           ifelse(a==1 | b==1, "AB",
              ifelse(a==2 | b==2, "BC", "CD")))

But it did not work. Also, I am not sure how to link the second part, starting from "else do" to R. I feel like SAS script is easier to understand and write in this case than R.

Aucun commentaire:

Enregistrer un commentaire