I want to create a tibble that has a column that is made of tibbles.
In particular, I want to do what the code below does but from i in 1:n_rows
. What is the best way of doing that?
library(dplyr)
library(tidyr)
n_rows <- 4000
n_cols <- 1000
# my data looks something like this
A <- matrix(data = rnorm(n=n_rows*n_cols), nrow = n_rows)
betas <- rnorm(n = n_rows)
# I want the first row of my tibble to look like this
i <- 1
my_data <- tibble(i=i,
beta = betas[i],
data = tibble(a = A[i,],
b = sample(c(1,2), size = n_cols, replace = T))) %>%
group_by(i, beta) %>%
nest()
my_data
#> # A tibble: 1 x 3
#> i beta data
#> <dbl> <dbl> <list>
#> 1 1 -2.26 <tibble [1,000 × 1]>
What is the tidy
way of doing this?
Created on 2019-05-24 by the reprex package (v0.2.1)