在R中优化if语句

时间:2017-04-10 11:20:06

标签: r

我有这段代码,虽然它有效 - 在我的data.frame中处理(7分钟)530,000条记录需要相当长的时间。

我的目标是在我的框架中创建一个字段,并根据people $ Month的值填充它,如下所示:

for (i in 1:nrow(people)) {
  if(people$Month[i]=='JAN') {
    people[i, 'new_month'] <- "1"
  }
  else if(people$Month[i]=='FEB') {
    people[i, 'new_month'] <- "2"
  }
  else if(people$Month[i]=='MAR') {
    people[i, 'new_month'] <- "3"
  }
  else if(people$Month[i]=='APR') {
    people[i, 'new_month'] <- "4"
  }
  else if(people$Month[i]=='MAY') {
    people[i, 'new_month'] <- "5"
  }
  else if(people$Month[i]=='JUN') {
    people[i, 'new_month'] <- "6"
  }
  else if(people$Month[i]=='JUL') {
    people[i, 'new_month'] <- "7"
  }
  else if(people$Month[i]=='AUG') {
    people[i, 'new_month'] <- "8"
  }
  else if(people$Month[i]=='SEP') {
    people[i, 'new_month'] <- "9"
  }
  else if(people$Month[i]=='OCT') {
    people[i, 'new_month'] <- "10"
  }
  else if(people$Month[i]=='NOV') {
    people[i, 'new_month'] <- "11"
  }
  else if(people$Month[i]=='DEC') {
    people[i, 'new_month'] <- "12"
  }
}

除非我在这里做过很多if语句,否则还有其他方法吗? 感谢。

1 个答案:

答案 0 :(得分:5)

match就是我要去的地方。

M <- sample(month.abb, size = 300, replace = TRUE)

people <- data.frame(Month = toupper(M))

people$new_month <- match(people$Month, toupper(month.abb))

head(people)

随机选择530,000个月的名字这样做大概需要10毫秒。