Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- A B C
- A B C 1 j i 100
- 1 j i 100 --> 2 j i 100
- 2 K P 101 3 K P 101
- 4 K P 101
- A B C
- [1,] "j" "i" "100"
- [2,] "j" "i" "100"
- [3,] "K" "P" "101"
- [4,] "K" "P" "101"
- df <- data.frame(a=1:2, b=letters[1:2])
- df[rep(seq_len(nrow(df)), each=2),]
- library(plyr)
- rep.row <- function(r, n){
- colwise(function(x) rep(x, n))(r)
- }
- library(mefa)
- rep(mtcars,10)
- mefa:::rep.data.frame(mtcars)
- rep(df, each=N)
- rep(df, times=N)
- > data <- data.frame(a=letters[1:3], b=letters[4:6])
- > data
- a b
- 1 a d
- 2 b e
- 3 c f
- > as.data.frame(lapply(data, rep, 2))
- a b
- 1 a d
- 2 b e
- 3 c f
- 4 a d
- 5 b e
- 6 c f
- N=2
- rep(1:4, each = N)
- rep.data.frame <- function(x, times) {
- rnames <- attr(x, "row.names")
- x <- lapply(x, rep.int, times = times)
- class(x) <- "data.frame"
- if (!is.numeric(rnames))
- attr(x, "row.names") <- make.unique(rep.int(rnames, times))
- else
- attr(x, "row.names") <- .set_row_names(length(rnames) * times)
- x
- }
- library(Lahman)
- library(microbenchmark)
- microbenchmark(
- mefa:::rep.data.frame(Batting, 10),
- rep.data.frame(Batting, 10),
- Batting[rep.int(seq_len(nrow(Batting)), 10), ],
- times = 10
- )
- #> Unit: milliseconds
- #> expr min lq mean median uq max neval cld
- #> mefa:::rep.data.frame(Batting, 10) 127.77786 135.3480 198.0240 148.1749 278.1066 356.3210 10 a
- #> rep.data.frame(Batting, 10) 79.70335 82.8165 134.0974 87.2587 191.1713 307.4567 10 a
- #> Batting[rep.int(seq_len(nrow(Batting)), 10), ] 895.73750 922.7059 981.8891 956.3463 1018.2411 1127.3927 10 b
- df$index = 1:nrow(df)
- df = rbind(df,df)
- df = df[order(df$index),][,-ncol(df)]
- library(dplyr)
- df <- data_frame(x = 1:2, y = c("a", "b"))
- df %>% slice(rep(1:n(), each = 2))
Add Comment
Please, Sign In to add comment