-1

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"
IceCreamToucan
  • 28,083
  • 2
  • 22
  • 38

1 Answers1

1

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)
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 2
    Just note that `spread` will work for simple cases like `dcast(a ~ b)`, but doesn't handle complex cases like `dcast(a + b ~ c + d)`. (at least not that I'm aware). There's talk of adding this to `tidyr` somewhere (can't find a link). – Benjamin Mar 14 '18 at 18:44