ifelse语句 - 根据条件添加变量

时间:2016-04-16 20:46:23

标签: r variables if-statement add

我正在尝试添加一个因子变量" Economy"水平"开发"和"开发"到我的数据集,其中包含国家/地区列表。

我做错了什么?

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 个答案:

答案 0 :(得分:0)

@DJJ,

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

工作得很好!问题解决了。