-2

I am an old dog struggling with R as a new trick

I want to replicate the follow SAS code in R (using a dataframe with multiple columns):-

if sum5 > 0 then gbind = 1;
else if  sum4 > 0 then gbind = 2;
else if block19 in ('B') then gbind = 3; (many other elements)
else gbind = 0;

Please Help

  • 2
    Did you try anything? Where exactly are you getting stuck? When asking for help, you should 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. Also: https://meta.stackoverflow.com/questions/265825/code-translation-tagging – MrFlick Mar 28 '18 at 15:39
  • If (newblocks201802a$sum5 > 0) {gbind1 < - 1 } else { If (newblocks201802$sum4 > 0) {gbind1 < - 2 } else gbind < - 0 } got unexpected } messages – Matthew Freeman Mar 28 '18 at 15:41

1 Answers1

0

I think the best solution would be to use ifelse. Without knowing the strcuture of your data this is the best I could come up with:

gbind <- ifelse(sum5 > 5, 1,
          ifelse(sum4 > 0 , 2,
          ifelse(block19 == 'B', 3,0)))

See this link for more details: ifelse

Mike
  • 3,797
  • 1
  • 11
  • 30