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

は完全に働きました!問題が解決しました。

関連する問題