美文网首页
1. R语言运行效率分析(7)

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

作者: 灭绝老衲 | 来源:发表于2020-02-06 20:52 被阅读0次

    方法7: 采用 join 语句

    1: 自定义函数

    Month_name_join<-function(month){
      d_month<-1:12
      d_month_name<-c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
      return(left_join(as.data.frame(month),data.frame(d_month,d_month_name),by=c("month"="d_month")))
    }
    Season_name_join<-function(month){
      d_month<-1:12
      d_season_name<-c("Winter","Winter","Spring","Spring","Spring","Summer","Summer","Summer","Autumn","Autumn","Autumn","Winter")
      return(left_join(as.data.frame(month),data.frame(d_month,d_season_name),by=c("month"="d_month")))
      
    }
    result_join<-function(month){
      d_month<-1:12
      d_month_name<-c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
      d_season_name<-c("Winter","Winter","Spring","Spring","Spring","Summer","Summer","Summer","Autumn","Autumn","Autumn","Winter")
      return(left_join(as.data.frame(month),data.frame(d_month,d_month_name,d_season_name),by=c("month"="d_month")))
    }
    

    2: 调用函数进行运算

    month<-month_digital(10)
    microbenchmark::microbenchmark(Month_name_join(month))
    microbenchmark::microbenchmark(Season_name_join(month))
    microbenchmark::microbenchmark(result_join(month))
    
    Unit: milliseconds
                       expr      min       lq     mean   median       uq      max
     Month_name_join(month) 1.979469 2.048706 2.467942 2.288968 2.432082 9.385545
     neval
       100
    Unit: milliseconds
                        expr    min       lq     mean   median       uq      max
     Season_name_join(month) 1.9838 2.141539 3.546876 2.985441 4.058224 13.25934
     neval
       100
    Unit: milliseconds
                   expr     min       lq     mean   median      uq      max neval
     result_join(month) 2.24823 2.290183 2.523177 2.323117 2.41993 10.02538   100
    

    (未完!待续……)

    相关文章

      网友评论

          本文标题:1. R语言运行效率分析(7)

          本文链接:https://www.haomeiwen.com/subject/yftpxhtx.html