我使用一系列if語句來檢查Year是否等於某個值,然後計算語句。有沒有更高效,更快捷的方法來執行以下內容而不是使用if語句?替代R語句中的if語句
for (i in 1:length(O$Year))
{
if (Year[i] == "2012") {
O$SD[i] = C[i]/5408666
} else if (Year[i] == "2013") {
O $SD[i] = C[i]/5300759
} else if (Year[i] == "2014") {
O$SD[i] = C[i]/5410924
} else if (Year[i] == "2015") {
O$SD[i] = C[i]/5446029
} else if (Year[i] == "2016") {
O$SD[i] = C[i]/5480869
} else
O$SD[i] = C[i]
}
**Data set named O**
**SD Year C**
43 2012 4
23 2012 5
12 2014 3
53 2014 3
那麼[switch](https://stat.ethz.ch/R-manual/R-devel/library/base/html/switch.html)怎麼樣? – user2100721