Guest User

Untitled

a guest
Oct 16th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. reprex::reprex_info()
  2. #> Created by the reprex package v0.1.1.9000 on 2017-10-16
  3.  
  4. library(dplyr, warn.conflicts = FALSE)
  5.  
  6. replace_cols1 <- function(df, cols, pos) {
  7. cbind(
  8. df[, 1:(pos-1), drop=FALSE],
  9. cols,
  10. df[, (pos+1):ncol(df), drop=FALSE]
  11. )
  12. }
  13.  
  14. replace_cols1_2 <- function(df, cols, pos) {
  15. bind_cols(
  16. df[, 1:(pos-1), drop=FALSE],
  17. cols,
  18. df[, (pos+1):ncol(df), drop=FALSE]
  19. )
  20. }
  21.  
  22. replace_cols2 <- function(df, cols, pos) {
  23. data.frame(append(df, cols, pos))[, -pos]
  24. }
  25.  
  26. replace_cols2_2 <- function(df, cols, pos) {
  27. as.data.frame(append(df, cols, pos))[, -pos]
  28. }
  29.  
  30. replace_cols3 <- function(df, cols, pos) {
  31. col_nm <- append(colnames(df)[-pos], names(cols), pos-1L)
  32. cbind(df, cols)[,col_nm]
  33. }
  34.  
  35. replace_cols3_2 <- function(df, cols, pos) {
  36. col_nm <- append(colnames(df)[-pos], names(cols), pos-1L)
  37. bind_cols(df, cols)[,col_nm]
  38. }
  39.  
  40.  
  41. df_part <- dplyr::select(iris, SL = Sepal.Length, PL = Petal.Length)
  42. microbenchmark::microbenchmark(
  43. replace_cols1(iris, df_part, 2L),
  44. replace_cols1_2(iris, df_part, 2L),
  45. replace_cols2(iris, df_part, 2L),
  46. replace_cols2_2(iris, df_part, 2L),
  47. replace_cols3(iris, df_part, 2L),
  48. replace_cols3_2(iris, df_part, 2L)
  49. )
  50. #> Unit: microseconds
  51. #> expr min lq mean median
  52. #> replace_cols1(iris, df_part, 2L) 102.321 125.4325 170.2560 139.8520
  53. #> replace_cols1_2(iris, df_part, 2L) 414.815 475.2600 662.5270 556.4455
  54. #> replace_cols2(iris, df_part, 2L) 567.310 610.7660 1024.4829 692.9385
  55. #> replace_cols2_2(iris, df_part, 2L) 525.827 586.2725 855.6373 655.0135
  56. #> replace_cols3(iris, df_part, 2L) 74.667 99.3580 157.4599 112.5930
  57. #> replace_cols3_2(iris, df_part, 2L) 354.371 399.0125 614.9774 507.2600
  58. #> uq max neval
  59. #> 168.0985 1004.247 100
  60. #> 741.1365 2518.916 100
  61. #> 953.4825 7756.254 100
  62. #> 991.2110 3482.472 100
  63. #> 161.5800 1084.840 100
  64. #> 632.6920 2453.730 100
  65.  
  66. big_iris <- iris[rep(seq_len(nrow(iris)), 100L),]
  67. big_df_part <- dplyr::select(big_iris, SL = Sepal.Length, PL = Petal.Length)
  68. microbenchmark::microbenchmark(
  69. replace_cols1(big_iris, big_df_part, 2L),
  70. replace_cols1_2(big_iris, big_df_part, 2L),
  71. replace_cols2(big_iris, big_df_part, 2L),
  72. replace_cols2_2(big_iris, big_df_part, 2L),
  73. replace_cols3(big_iris, big_df_part, 2L),
  74. replace_cols3_2(big_iris, big_df_part, 2L)
  75. )
  76. #> Unit: microseconds
  77. #> expr min lq mean
  78. #> replace_cols1(big_iris, big_df_part, 2L) 780.643 956.0500 2302.5244
  79. #> replace_cols1_2(big_iris, big_df_part, 2L) 423.902 538.0750 1195.2366
  80. #> replace_cols2(big_iris, big_df_part, 2L) 578.371 676.7410 1569.6369
  81. #> replace_cols2_2(big_iris, big_df_part, 2L) 519.112 610.9630 1405.0698
  82. #> replace_cols3(big_iris, big_df_part, 2L) 726.915 863.4080 1946.8223
  83. #> replace_cols3_2(big_iris, big_df_part, 2L) 361.482 490.6675 933.9741
  84. #> median uq max neval
  85. #> 1348.7415 2145.7795 13209.69 100
  86. #> 679.9020 1038.8155 28910.24 100
  87. #> 778.6675 1154.3715 34127.83 100
  88. #> 761.2850 1179.6555 16451.57 100
  89. #> 1125.3345 1932.4460 16254.43 100
  90. #> 611.7535 900.5435 10620.06 100
Add Comment
Please, Sign In to add comment