1. R语言运行效率分析 (3)

方法3: 采用 for + ifelse 语句

1: 自定义函数

# digital was translated into englishname
Month_name_for_ifelse<-function(month){
  Month_name<-c()
  for (i in 1:length(month)){
    ifelse (month[i]==1, Month_name[i]<-"Jan", 
            ifelse (month[i]==2, Month_name[i]<-"Feb",
                    ifelse (month[i]==3, Month_name[i]<-"Mar",
                            ifelse (month[i]==4, Month_name[i]<-"Apr",
                                    ifelse (month[i]==5, Month_name[i]<-"May",
                                            ifelse (month[i]==6, Month_name[i]<-"Jun",
                                                    ifelse (month[i]==7, Month_name[i]<-"Jul",
                                                            ifelse (month[i]==8, Month_name[i]<-"Aug",
                                                                    ifelse (month[i]==9, Month_name[i]<-"sep",
                                                                            ifelse (month[i]==10, Month_name[i]<-"Oct",
                                                                                      ifelse (month[i]==11, Month_name[i]<-"Nov",
                                                                                              Month_name[i]<-"Dec")
                                                                            )
                                                                    )
                                                            )
                                                    )
                                            )
                                    )
                            )
                    )
            )
    )
            
  }
  return(Month_name)
}
# season data
Season_name_for_ifelse<-function(month){
  Season_name<-c()
  for (i in 1:length(month)){
    ifelse((month[i]==1 | month[i]==2 | month[i]==12), Season_name[i]<-"Winter",
            ifelse ((month[i]==3|month[i]==4|month[i]==5), Season_name[i]<-"Spring",
                    ifelse ((month[i]==6|month[i]==7|month[i]==8), Season_name[i]<-"Summer",
                            Season_name[i]<-"autumn")
            )
    )
  }
  return(Season_name)
}
result_for_ifelse<-function(month){
  Month_name_for_ifelse<-Month_name_for_ifelse(month)# months' names
  Season_name_for_ifelse<-Season_name_for_ifelse(month) #seasons' names
  df<-data.frame(month,Month_name_for_ifelse,Season_name_for_ifelse)
  return(df)
}

2: 调用函数进行运算

month<-month_digital(10)
microbenchmark::microbenchmark(Month_name_for_ifelse(month))
microbenchmark::microbenchmark(Season_name_for_ifelse(month))
microbenchmark::microbenchmark(result_for_ifelse(month))
Unit: microseconds
                         expr     min       lq     mean   median       uq
 Month_name_for_ifelse(month) 176.362 181.3845 516.0331 190.1935 196.6265
      max neval
 32033.72   100
Unit: microseconds
                          expr    min      lq     mean median    uq      max
 Season_name_for_ifelse(month) 83.435 85.2445 316.2032 89.358 92.34 22765.35
 neval
   100
Unit: microseconds
                     expr     min       lq     mean   median      uq      max
 result_for_ifelse(month) 906.764 916.8955 1007.159 931.0235 953.935 4943.058
 neval
   100

(未完,待续……)

发布了13 篇原创文章 · 获赞 14 · 访问量 709

猜你喜欢

转载自blog.csdn.net/fan_xiao_hui/article/details/104146435