且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用* pply替代替换循环

更新时间:2022-12-09 14:48:38

You may try ave:

DF$series <- ave(DF$id, DF$id, FUN = seq_along)

For larger data sets, dplyr is faster though.

library(dplyr)

fun_ave <- function(df) transform(df, series = ave(id, id, FUN = seq_along))

fun_dp <- function(df) df %.%
                 group_by(id) %.%
                 mutate(
                   series = seq_along(id))

df <- data.frame(id= sample(letters[1:3], 100000, replace = TRUE))

microbenchmark(fun_ave(df))
# Unit: milliseconds
#        expr      min       lq   median      uq      max neval
# fun_ave(df) 38.59112 39.40802 50.77921 51.2844 128.6791   100


microbenchmark(fun_dp(df))
# Unit: milliseconds
#       expr      min       lq   median       uq      max neval
# fun_dp(df) 4.977035 5.034244 5.060663 5.265173 17.16018   100

相关阅读

技术问答最新文章