亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嵌套ifasser語句

嵌套ifasser語句

嗶嗶one 2019-07-06 17:48:21
嵌套ifasser語句我仍在學習如何將SAS代碼轉換為R,并收到警告。我需要弄清楚我在哪里犯錯誤。我想要做的是創建一個變量來總結和區分一個人口的三個狀態:大陸,海外,外國人。我有一個包含兩個變量的數據庫:ID國籍:idnat(法語、外國人)、如果idnat那么法語是:身份證出生地:idbp(內地、殖民地、海外)我想總結一下idnat和idbp到一個名為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,因此只考慮第一個括號之間的內容。對不起,我不明白這個長度和這里有什么關系?有人知道我哪里錯了嗎?
查看完整描述

3 回答

?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

嘗試如下所示:

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

您的困惑來自SAS和R如何處理if-etc結構。在R,ifelse沒有向量化,這意味著它們檢查單個條件是否為真(即,if("french"=="french")無法處理多個邏輯(即,if(c("french","foreigner")=="french")R給你收到的警告。

相比之下,ifelse是矢量化的,因此它可以接受向量(也稱為輸入變量),并測試每個元素的邏輯條件,就像您習慣于在SAS中那樣。另一種讓你頭腦清醒的方法是用以下方法構建一個循環ifelse語句(就像您在這里開始做的那樣),但是ifelse方法將更有效率,涉及的代碼通常更少。


查看完整回答
反對 回復 2019-07-06
?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

如果數據集包含許多行,則使用data.table而不是嵌套ifelse().


提供了下面的查找表


lookup

     idnat     idbp   idnat2

1:  french mainland mainland

2:  french   colony overseas

3:  french overseas overseas

4: foreign  foreign  foreign

和一個樣本數據集


library(data.table)

n_row <- 10L

set.seed(1L)

DT <- data.table(idnat = "french",

                 idbp = sample(c("mainland", "colony", "overseas", "foreign"), n_row, replace = TRUE))

DT[idbp == "foreign", idnat := "foreign"][]

      idnat     idbp

 1:  french   colony

 2:  french   colony

 3:  french overseas

 4: foreign  foreign

 5:  french mainland

 6: foreign  foreign

 7: foreign  foreign

 8:  french overseas

 9:  french overseas

10:  french mainland

然后我們可以做一個加入時更新:


DT[lookup, on = .(idnat, idbp), idnat2 := i.idnat2][]

      idnat     idbp   idnat2

 1:  french   colony overseas

 2:  french   colony overseas

 3:  french overseas overseas

 4: foreign  foreign  foreign

 5:  french mainland mainland

 6: foreign  foreign  foreign

 7: foreign  foreign  foreign

 8:  french overseas overseas

 9:  french overseas overseas

10:  french mainland mainland


查看完整回答
反對 回復 2019-07-06
  • 3 回答
  • 0 關注
  • 571 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號