-1

Is it possible to set a limit to the amount of columns tidyr::spread creates that is less than biggest_group_size?

Nick Resnick
  • 591
  • 1
  • 5
  • 14
  • 1
    It would be easier to help if you provided some sort of [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output. – MrFlick Aug 02 '16 at 19:50

1 Answers1

1

Not entirely clear about the greater than the biggest_group_size. If we want to have to have columns with NA or "", use the drop = FALSE argument in spread after changing the 'id' to factor with extra levels specified

library(dplyr)
library(tidyr)
df1 %>% 
   group_by(user_id) %>%
   mutate(id = paste0("pred_", row_number()), 
          id = factor(id, levels = paste0("pred_", 1:4))) %>%
   spread(id, pred, drop=FALSE, fill = "")
akrun
  • 874,273
  • 37
  • 540
  • 662
  • What I meant was to limit the amount of spread to be `N`, where `N` < `biggest_group_size`. I'm using this for a holdout period where I arrange first by `date` and then want only the first `N` observations to be spread out – Nick Resnick Aug 02 '16 at 19:28
  • @NickResnick Please include a small example and expected output in your post for better understanding. – akrun Aug 03 '16 at 02:51