Search code examples
rtidyversereshape2tibbledcast

Keep tibbles as tibbles after reshape2::dcast


Is there a way to get dcast to output a tibble? (without using as_tibble)

df <- tibble(a = letters[c(rep(1:3, 3), 1)], b =  1:10 + .1, c = 11:20 + .1)

df %>% class
# [1] "tbl_df"     "tbl"        "data.frame"
df %>% dcast(a ~ b) %>% class
# [1] "data.frame"

Solution

  • The reshape2 package has been retired and will unlikely be updated to support tibbles. If you want to stick with tibbles, you should use the tidyverse version of the reshape2 package which is called tidyr. You can use

    library(tidyr)
    df %>% spread(b, c)