group_by n变量的唯一顺序值

los*_*ost 2 r dplyr

group_by变量的唯一值很容易:

library(tidyverse)
library(gapminder)
gapminder %>%
  group_by(year)
Run Code Online (Sandbox Code Playgroud)

如果我们想创建一个组ID只是为了向我们展示这些组将是什么:

gapminder %>% 
  select(year) %>% 
  distinct %>%
  mutate(group = group_indices(., year))
A tibble: 12 x 2
    year group
   <int> <int>
 1  1952     1
 2  1957     2
 3  1962     3
 4  1967     4
 5  1972     5
 6  1977     6
 7  1982     7
 8  1987     8
 9  1992     9
10  1997    10
11  2002    11
12  2007    12
Run Code Online (Sandbox Code Playgroud)

但是,如果我想按顺序分组("group2"),三胞胎("group3")等分组呢?我怎么能用dplyr/ tidyverse?产生以下的tibble ?

A tibble: 12 x 2
    year group group2 group3 group5
   <int> <int>  <int>  <int>  <int>
 1  1952     1      1      1      1
 2  1957     2      1      1      1
 3  1962     3      2      1      1
 4  1967     4      2      2      1
 5  1972     5      3      2      1
 6  1977     6      3      2      2
 7  1982     7      4      3      2
 8  1987     8      4      3      2
 9  1992     9      5      3      2
10  1997    10      5      4      2
11  2002    11      6      4      3
12  2007    12      6      4      3
Run Code Online (Sandbox Code Playgroud)

小智 5

有了ceiling()你可以非常轻松地创建组.

gapminder %>% 
    select(year) %>% 
    distinct() %>%
    mutate(group1 = group_indices(., year)) %>%
    mutate(group2=ceiling(group1 / 2)) %>%
    mutate(group3=ceiling(group1 / 3)) %>%
    mutate(group4=ceiling(group1 / 4)) %>%
    mutate(group5=ceiling(group1 / 5))

# A tibble: 12 x 6
    year group1 group2 group3 group4 group5
   <int>  <int>  <dbl>  <dbl>  <dbl>  <dbl>
 1  1952      1      1      1      1      1
 2  1957      2      1      1      1      1
 3  1962      3      2      1      1      1
 4  1967      4      2      2      1      1
 5  1972      5      3      2      2      1
 6  1977      6      3      2      2      2
 7  1982      7      4      3      2      2
 8  1987      8      4      3      2      2
 9  1992      9      5      3      3      2
10  1997     10      5      4      3      2
11  2002     11      6      4      3      3
12  2007     12      6      4      3      3
Run Code Online (Sandbox Code Playgroud)