嵌套ifasser语句

嵌套ifasser语句

我仍在学习如何将SAS代码转换为R,并收到警告。我需要弄清楚我在哪里犯错误。我想要做的是创建一个变量来总结和区分一个人口的三个状态:大陆,海外,外国人。我有一个包含两个变量的数据库:

  • ID国籍:

    idnat

    (法语、外国人)、

如果idnat那么法语是:

  • 身份证出生地:

    idbp

    (内地、殖民地、海外)

我想总结一下idnatidbp到一个名为idnat2:

  • 地位:K(内地、海外、外国人)

所有这些变量都使用“字符类型”。

第2栏预期的结果:

   idnat     idbp   idnat21  french mainland mainland2  french   colony overseas3  french overseas overseas4 foreign 
    foreign  foreign

下面是我想在R中翻译的SAS代码:

if idnat = "french" then do;
   if idbp in ("overseas","colony") then idnat2 = "overseas";
   else idnat2 = "mainland";end;else idnat2 = "foreigner";run;

以下是我在R中的尝试:

if(idnat=="french"){
    idnat2 <- "mainland"} else if(idbp=="overseas"|idbp=="colony"){
    idnat2 <- "overseas"} else {
    idnat2 <- "foreigner"}

我收到这样的警告:

Warning message:In if (idnat=="french") { :
  the condition has length > 1 and only the first element will be used

我被建议使用“嵌套”ifelse“相反,它的轻松,但得到更多的警告:

idnat2 <- ifelse (idnat=="french", "mainland",
        ifelse (idbp=="overseas"|idbp=="colony", "overseas")
      )
            else (idnat2 <- "foreigner")

根据警告消息,长度大于1,因此只考虑第一个括号之间的内容。对不起,我不明白这个长度和这里有什么关系?有人知道我哪里错了吗?


哔哔one
浏览 505回答 3
3回答

胡说叔叔

尝试如下所示:#&nbsp;some&nbsp;sample&nbsp;dataidnat&nbsp;<-&nbsp;sample(c("french","foreigner"),100,TRUE)idbp&nbsp;<-&nbsp;rep(NA,100)idbp[idnat=="french"] &nbsp;<-&nbsp;sample(c("mainland","overseas","colony"),sum(idnat=="french"),TRUE)#&nbsp;recodingout&nbsp;<-&nbsp;ifelse(idnat=="french"&nbsp;& &nbsp;&nbsp;!idbp&nbsp;%in%&nbsp;c("overseas","colony"),&nbsp;"mainland", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ifelse(idbp&nbsp;%in%&nbsp;c("overseas","colony"),"overseas", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"foreigner"))cbind(idnat,idbp,out)&nbsp;#&nbsp;check&nbsp;result您的困惑来自SAS和R如何处理if-etc结构。在R,if和else没有向量化,这意味着它们检查单个条件是否为真(即,if("french"=="french")无法处理多个逻辑(即,if(c("french","foreigner")=="french")R给你收到的警告。相比之下,ifelse是矢量化的,因此它可以接受向量(也称为输入变量),并测试每个元素的逻辑条件,就像您习惯于在SAS中那样。另一种让你头脑清醒的方法是用以下方法构建一个循环if和else语句(就像您在这里开始做的那样),但是ifelse方法将更有效率,涉及的代码通常更少。

慕运维8079593

如果数据集包含许多行,则使用data.table而不是嵌套ifelse().提供了下面的查找表lookup&nbsp; &nbsp; &nbsp;idnat&nbsp; &nbsp; &nbsp;idbp&nbsp; &nbsp;idnat21:&nbsp; french mainland mainland2:&nbsp; french&nbsp; &nbsp;colony overseas3:&nbsp; french overseas overseas4: foreign&nbsp; foreign&nbsp; foreign和一个样本数据集library(data.table)n_row <- 10Lset.seed(1L)DT <- data.table(idnat = "french",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;idbp = sample(c("mainland", "colony", "overseas", "foreign"), n_row, replace = TRUE))DT[idbp == "foreign", idnat := "foreign"][]&nbsp; &nbsp; &nbsp; idnat&nbsp; &nbsp; &nbsp;idbp&nbsp;1:&nbsp; french&nbsp; &nbsp;colony&nbsp;2:&nbsp; french&nbsp; &nbsp;colony&nbsp;3:&nbsp; french overseas&nbsp;4: foreign&nbsp; foreign&nbsp;5:&nbsp; french mainland&nbsp;6: foreign&nbsp; foreign&nbsp;7: foreign&nbsp; foreign&nbsp;8:&nbsp; french overseas&nbsp;9:&nbsp; french overseas10:&nbsp; french mainland然后我们可以做一个加入时更新:DT[lookup, on = .(idnat, idbp), idnat2 := i.idnat2][]&nbsp; &nbsp; &nbsp; idnat&nbsp; &nbsp; &nbsp;idbp&nbsp; &nbsp;idnat2&nbsp;1:&nbsp; french&nbsp; &nbsp;colony overseas&nbsp;2:&nbsp; french&nbsp; &nbsp;colony overseas&nbsp;3:&nbsp; french overseas overseas&nbsp;4: foreign&nbsp; foreign&nbsp; foreign&nbsp;5:&nbsp; french mainland mainland&nbsp;6: foreign&nbsp; foreign&nbsp; foreign&nbsp;7: foreign&nbsp; foreign&nbsp; foreign&nbsp;8:&nbsp; french overseas overseas&nbsp;9:&nbsp; french overseas overseas10:&nbsp; french mainland mainland
打开App,查看更多内容
随时随地看视频慕课网APP