Skip to content

Instantly share code, notes, and snippets.

@yutannihilation
Last active October 16, 2017 11:13
Show Gist options
  • Select an option

  • Save yutannihilation/4e2d63e60d6ba101e79df6fad06304d6 to your computer and use it in GitHub Desktop.

Select an option

Save yutannihilation/4e2d63e60d6ba101e79df6fad06304d6 to your computer and use it in GitHub Desktop.
reprex::reprex_info()
#> Created by the reprex package v0.1.1.9000 on 2017-10-16
library(dplyr, warn.conflicts = FALSE)
replace_cols1 <- function(df, cols, pos) {
cbind(
df[, 1:(pos-1), drop=FALSE],
cols,
df[, (pos+1):ncol(df), drop=FALSE]
)
}
replace_cols1_2 <- function(df, cols, pos) {
bind_cols(
df[, 1:(pos-1), drop=FALSE],
cols,
df[, (pos+1):ncol(df), drop=FALSE]
)
}
replace_cols2 <- function(df, cols, pos) {
data.frame(append(df, cols, pos))[, -pos]
}
replace_cols2_2 <- function(df, cols, pos) {
as.data.frame(append(df, cols, pos))[, -pos]
}
replace_cols3 <- function(df, cols, pos) {
col_nm <- append(colnames(df)[-pos], names(cols), pos-1L)
cbind(df, cols)[,col_nm]
}
replace_cols3_2 <- function(df, cols, pos) {
col_nm <- append(colnames(df)[-pos], names(cols), pos-1L)
bind_cols(df, cols)[,col_nm]
}
df_part <- dplyr::select(iris, SL = Sepal.Length, PL = Petal.Length)
microbenchmark::microbenchmark(
replace_cols1(iris, df_part, 2L),
replace_cols1_2(iris, df_part, 2L),
replace_cols2(iris, df_part, 2L),
replace_cols2_2(iris, df_part, 2L),
replace_cols3(iris, df_part, 2L),
replace_cols3_2(iris, df_part, 2L)
)
#> Unit: microseconds
#> expr min lq mean median
#> replace_cols1(iris, df_part, 2L) 102.321 125.4325 170.2560 139.8520
#> replace_cols1_2(iris, df_part, 2L) 414.815 475.2600 662.5270 556.4455
#> replace_cols2(iris, df_part, 2L) 567.310 610.7660 1024.4829 692.9385
#> replace_cols2_2(iris, df_part, 2L) 525.827 586.2725 855.6373 655.0135
#> replace_cols3(iris, df_part, 2L) 74.667 99.3580 157.4599 112.5930
#> replace_cols3_2(iris, df_part, 2L) 354.371 399.0125 614.9774 507.2600
#> uq max neval
#> 168.0985 1004.247 100
#> 741.1365 2518.916 100
#> 953.4825 7756.254 100
#> 991.2110 3482.472 100
#> 161.5800 1084.840 100
#> 632.6920 2453.730 100
big_iris <- iris[rep(seq_len(nrow(iris)), 100L),]
big_df_part <- dplyr::select(big_iris, SL = Sepal.Length, PL = Petal.Length)
microbenchmark::microbenchmark(
replace_cols1(big_iris, big_df_part, 2L),
replace_cols1_2(big_iris, big_df_part, 2L),
replace_cols2(big_iris, big_df_part, 2L),
replace_cols2_2(big_iris, big_df_part, 2L),
replace_cols3(big_iris, big_df_part, 2L),
replace_cols3_2(big_iris, big_df_part, 2L)
)
#> Unit: microseconds
#> expr min lq mean
#> replace_cols1(big_iris, big_df_part, 2L) 780.643 956.0500 2302.5244
#> replace_cols1_2(big_iris, big_df_part, 2L) 423.902 538.0750 1195.2366
#> replace_cols2(big_iris, big_df_part, 2L) 578.371 676.7410 1569.6369
#> replace_cols2_2(big_iris, big_df_part, 2L) 519.112 610.9630 1405.0698
#> replace_cols3(big_iris, big_df_part, 2L) 726.915 863.4080 1946.8223
#> replace_cols3_2(big_iris, big_df_part, 2L) 361.482 490.6675 933.9741
#> median uq max neval
#> 1348.7415 2145.7795 13209.69 100
#> 679.9020 1038.8155 28910.24 100
#> 778.6675 1154.3715 34127.83 100
#> 761.2850 1179.6555 16451.57 100
#> 1125.3345 1932.4460 16254.43 100
#> 611.7535 900.5435 10620.06 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment