library(tidyverse)
## make data
dat <- matrix(rnorm(100, 3, 1), ncol=10)
## reshape data (tidy/tall form)
dat2 <- dat %>%
tbl_df() %>%
rownames_to_column('Var1') %>%
gather(Var2, value, -Var1) %>%
mutate(
Var1 = factor(Var1, levels=1:10),
Var2 = factor(gsub("V", "", Var2), levels=1:10)
)
I assume that worked in the past but now it throws an error:
Error:
! Columns 1, 2, 3, 4, 5, and 5 more must be named.
Use .name_repair to specify repair.
Caused by error in `repaired_names()`:
! Names can't be empty.
✖ Empty names found at locations 1, 2, 3, 4, 5, etc.
The default behavior is what is expected by the gsub() step, if you use a different .name_repair option you would need to adjust the regular expression accordingly.
In your example the source object, dat, is a matrix of 10 columns, there are no names as such.
Therefore creating a tibble would require defining some names...
the name_repair strategies are "check_unique", "unique","universal","minimal" and actually a NULL strategy - that is probably the one what you will find most convenient. Here is a short code to illustrate how they would create names based off of the dat input
library(tidyverse)
dat <- matrix(rnorm(100, 3, 1), ncol=10)
name_rep <- list(NULL,"check_unique",
"unique", "universal", "minimal")
safe_as_tibble <- purrr::safely(as_tibble,
otherwise = list(failed="Failed"))
names_list <- map(name_rep,
~dat %>%
safe_as_tibble(.name_repair = .x) %>%
pluck("result") %>%
names()) %>%
set_names(name_rep)
#### what are the names each .name_repair strategy came up with ?
names_list
check_unique is perhaps the odd one out in that it is more concerned with failing out if the input names are not unique rather than creating names
you can be explicit and define your own nameing convention for something like dat for example