For example, the input here has two columns (and zero rows), but the output has only one column:
dat <- data.frame(x = numeric(0), g = character(0)) dat %>% group_by(g) %>% do(identity(.)) %>% str() # Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 0 obs. of 1 variable: # $ g: Factor w/ 0 levels: # - attr(*, "vars")=List of 1 # ..$ : symbol g # - attr(*, "drop")= logi TRUE # - attr(*, "indices")= list() # - attr(*, "group_sizes")= int # - attr(*, "biggest_group_size")= int 0 # - attr(*, "labels")='data.frame': 0 obs. of 1 variable: # ..$ g: Factor w/ 0 levels: # ..- attr(*, "vars")=List of 1 # .. ..$ : symbol g
In my particular case, I would like it to simply return the input unchanged, but I'm not sure what the correct output should be, in general.
Since the function isn't actually called on zero-row data, there's no way of knowing what columns it would return. Maybe it should be called once with the zero-row data frame? In this case, that would be equivalent to doing dat %>% group_by(g) %>% identity().