Skip to content

Instantly share code, notes, and snippets.

@TobCap
Created August 21, 2018 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TobCap/d91346622cf74b696da30357da3170b0 to your computer and use it in GitHub Desktop.
Save TobCap/d91346622cf74b696da30357da3170b0 to your computer and use it in GitHub Desktop.
repeat_dataframe
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