2016-04-16 89 views
-2

我試圖添加一個因子變量「經濟」,其中包含「開發」和「開發」級別的數據集,其中包含一個國家/地區列表。ifelse聲明 - 根據條件添加一個變量

我在做什麼錯?

Developed <- data.frame(c("Andorra", "Faroe Islands", "Ireland", "Monaco", "Spain", "Australia", "Finland", 
      "Israel", "Netherlands", "Sweden", "Austria", "France", "Italy", "New Zealand", "Switzerland", 
      "Belgium", "Germany", "Japan", "Norway", "Turkey", "Bermuda", "Greece", "Liechtenstein", 
      "Portugal", "United Kingdom", "Canada", "Holy See", "Luxembourg", "San Marino", "United States", 
      "Denmark", "Iceland", "Malta", "South Africa", "Hong Kong", "South Korea", "Singapore", "Taiwan")) 
names(Developed) <- "Country" 

total$Economy <- ifelse(d$Country==Developed$Country, "Developed", "Developing") 

它產生以下錯誤:

Warning messages: 
1: In is.na(e1) | is.na(e2) : 
longer object length is not a multiple of shorter object length 
2: In `==.default`(data$Country, Developed$Country) : 
longer object length is not a multiple of shorter object length 
+1

這裏的'd'是什麼?請使代碼可重現。 – Psidom

+1

這是告訴你,你有''''比'發展'更多的國家。如果在使用'=='時長度不匹配,它會拋出這個錯誤。試試'ifelse(d $ Country%in%Developed $ Country,「Developed」,「Developing」)' – DJJ

回答

0

@DJJ,

ifelse(d$Country%in%Developed$Country, "Developed", "Developing") 

完美!問題解決了。