In dplyr 0.5.0, in a grouped data frame, why does slice (1) not give the same row order as the filter (row_number () == 1)?

I observe that slicereorders strings in some cases when used group_by.

tmp_df2 <- data.frame(a = c(1, 3, 2, 4), b = c(1, 2, 3, 4))

tmp_df2 %>%
    group_by(a) %>%
    slice(1)

gives

Source: local data frame [4 x 2]
Groups: a [4]

      a     b
  <dbl> <dbl>
1     1     1
2     2     3
3     3     2
4     4     4

and

tmp_df2 %>%
    group_by(a) %>%
    filter(row_number() == 1)

gives

Source: local data frame [4 x 2]
Groups: a [4]

      a     b
  <dbl> <dbl>
1     1     1
2     3     2
3     2     3
4     4     4

It seems that it slicereconfigures the output in ascending order of grouping variables. However, the documentation suggests that the slicefilter should behave the same, especially from ?slice(my selection):

Slice does not work with relational databases because they do not have an internal concept of row order. If you want to perform an equivalent operation, use filter () and row_number ().

+4
source share
1 answer

, slice() , . , , filter, , .

, , ,

+2

Source: https://habr.com/ru/post/1658512/


All Articles