我有,列出了每個站點如何在R中應用多個if語句?
Site Country
Site1 USA
Site2 Vietnam
Site3 Spain
Site4 Germany
Site5 China
我要附加列,其中每個國家我其相應的關聯大陸相關的國家數據幀(DF)。我寫了一個簡單if loop
做到這一點:
df$Continent <- NA
if(df$Country == "USA" |df$Country == "Canada" |df$Country == "Mexico")
{df$Continent <- "North America"}
if(df$Country == "Spain" |df$Country == "France" |df$Country == "Germany")
{df$Continent <- "Europe"}
## .. etc
summary(df)
然而,每次我運行它的DF,我發現它分配北美的所有國家。我知道這可能聽起來微不足道,但是如果我在全球各地使用if
陳述而不是else
或if else
,它會有所幫助嗎?任何建議來糾正這個?
'if'和'ifelse'根本不相同。你可能更好使用查找表的排序 - http://stackoverflow.com/questions/18456968/how-do-i-map-a-vector-of-values-to-another-vector-with-我的自定義地圖在r/18457055 – thelatemail