How can I create a tibble using purrr. These are my not working tries:
library(tidyverse)
vec <- set_names(1:4, letters[1:4])
map_dfr(vec, ~rep(0, 10)) # not working
bind_rows(map(vec, ~rep(0, 10))) # not working either
This would be my base R solution, but I would like to do it "tidy":
do.call(rbind, lapply(vec, function(x) rep(0, 10)))
#[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#a 0 0 0 0 0 0 0 0 0 0
#b 0 0 0 0 0 0 0 0 0 0
#c 0 0 0 0 0 0 0 0 0 0
#d 0 0 0 0 0 0 0 0 0 0
Please note that the rep-function is not the full function I will use. This would not be a preferred solution for my problem:
as_tibble(matrix(rep(0, 40), nrow = 4, dimnames = list(letters[1:4])))
Thx & kind regards
tibble
cause it indicates you are using wide, not long format. But you can generate your data separately, usebind_rows
to generate atibble
and then set the rownames later – Julian_Hn