R: SAS (if/then statement )in R

后端 未结 4 1378
遥遥无期
遥遥无期 2021-01-16 17:28

I was working previously with SAS and then decided to shift to R for academic requirements reasons. My data (healthdemo) are health data containing some health diagnostic co

4条回答
  •  滥情空心
    2021-01-16 18:08

    The behavior of IF ... THEN >>> in SAS is achieved by the use NOT of if(...){...} but rather of ifelse(..., ..., ...). And you cannot use the form a < var < b. Furthermore you have not quite gotten the functional paradigm of R programming.

    Try this instead your last statement:

    healthdemo$cvd <- NA   # initialize to missing
    healthdemo$cvd <- ifelse (healthdemo$icd_char == "I" & 
                               01 <= healthdemo$icd_num &
                               healthdemo$icd_num < 52 , 1, healthdemo$cvd ) 
    

    Note that the form: var <- ifelse(logicalvec, value, var) allows you to do selective replacements. The old value is the default and only the "parallel" value of TRUE in the logical vector triggers a change.

    Robert Muenchen has written a book entitled something along the lines of 'R for SAS and SPSS Users'. There's also a freely available draft version that about 70 page long that should show up with a web search.

提交回复
热议问题