Nested ifelse statement

前端 未结 9 1211
逝去的感伤
逝去的感伤 2020-11-22 04:02

I\'m still learning how to translate a SAS code into R and I get warnings. I need to understand where I\'m making mistakes. What I want to do is create a variable which summ

9条回答
  •  终归单人心
    2020-11-22 04:21

    Try something like the following:

    # some sample data
    idnat <- sample(c("french","foreigner"),100,TRUE)
    idbp <- rep(NA,100)
    idbp[idnat=="french"] <- sample(c("mainland","overseas","colony"),sum(idnat=="french"),TRUE)
    
    # recoding
    out <- ifelse(idnat=="french" & !idbp %in% c("overseas","colony"), "mainland",
                  ifelse(idbp %in% c("overseas","colony"),"overseas",
                         "foreigner"))
    cbind(idnat,idbp,out) # check result
    

    Your confusion comes from how SAS and R handle if-else constructions. In R, if and else are not vectorized, meaning they check whether a single condition is true (i.e., if("french"=="french") works) and cannot handle multiple logicals (i.e., if(c("french","foreigner")=="french") doesn't work) and R gives you the warning you're receiving.

    By contrast, ifelse is vectorized, so it can take your vectors (aka input variables) and test the logical condition on each of their elements, like you're used to in SAS. An alternative way to wrap your head around this would be to build a loop using if and else statements (as you've started to do here) but the vectorized ifelse approach will be more efficient and involve generally less code.

提交回复
热议问题