Created
August 21, 2018 06:24
-
-
Save TobCap/d91346622cf74b696da30357da3170b0 to your computer and use it in GitHub Desktop.
repeat_dataframe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
A <- data.frame(x = 1:3, y = letters[1:3], stringsAsFactors = FALSE) | |
shape <- matrix(1, 2, 3) | |
rep_df <- function(df_, shape) { | |
df_ %>% | |
list() %>% | |
matrix %>% | |
kronecker(shape, FUN = rep.int) %>% | |
as_tibble() %>% | |
unnest() | |
} | |
rep_df(A, shape) | |
## A tibble: 6 x 6 | |
# x y x1 y1 x2 y2 | |
# <int> <chr> <int> <chr> <int> <chr> | |
#1 1 a 1 a 1 a | |
#2 2 b 2 b 2 b | |
#3 3 c 3 c 3 c | |
#4 1 a 1 a 1 a | |
#5 2 b 2 b 2 b | |
#6 3 c 3 c 3 c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment