2017-03-29 119 views
0

以下問題中「Equals」的運算符是什麼?使用嵌套ifelse複製VLOOKUP

S1$CountryNo <- ifelse(S1$Country = 'UNITED KINGDOM' , 1 , ifelse(S1$Country = 'FRANCE' , 2 , 3) 
+2

你需要使用的''==而不是'=' –

+0

這麼簡單!謝謝 – Quacks101

回答

0

如果你有個縣的一個長長的清單,你也可以考慮查找向量:

S1 <- data.frame(CountryNo = c("UNITED KINGDOM", "FRANCE", "DENMARK")) 

# Lookup vector 
lookup <- c("UNITED KINGDOM" = 1, "FRANCE" = 2) 

# Replace known countries - unknown (DENMARK) will be NA 
S1$CountryNo <- lookup[S1$CountryNo] 

# Replace NA countries with 3 
S1[is.na(S1$CountryNo), "CountryNo"] <- 3 
+0

謝謝你,那將是我的下一個學習點! – Quacks101