Nested ifelse statement

前端 未结 9 1214
逝去的感伤
逝去的感伤 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:34

    # Read in the data.
    
    idnat=c("french","french","french","foreign")
    idbp=c("mainland","colony","overseas","foreign")
    
    # Initialize the new variable.
    
    idnat2=as.character(vector())
    
    # Logically evaluate "idnat" and "idbp" for each case, assigning the appropriate level to "idnat2".
    
    for(i in 1:length(idnat)) {
      if(idnat[i] == "french" & idbp[i] == "mainland") {
        idnat2[i] = "mainland"
    } else if (idnat[i] == "french" & (idbp[i] == "colony" | idbp[i] == "overseas")) {
      idnat2[i] = "overseas"
    } else {
      idnat2[i] = "foreign"
    } 
    }
    
    # Create a data frame with the two old variables and the new variable.
    
    data.frame(idnat,idbp,idnat2) 
    

提交回复
热议问题