2016-04-16 90 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") 

完美!问题解决了。