如何通过 R 中的第二个分组变量分配 "reseting" 组号?

How to assign a "reseting" group number by the second grouping variable in R?

我的数据是这样的:

Measurement Compound Measure
1 A 111
1 A 222
1 B 333
1 B 444
2 C 555
2 C 666
2 D 777
2 D 888

我正在尝试根据化合物分配一个“重置”组号:

Measurement Compound Measure Compound_order
1 A 111 1
1 A 222 1
1 B 333 2
1 B 444 2
2 C 555 1
2 C 666 1
2 D 777 2
2 D 888 2

没有想出关于 dplyr 的解决方案 group_by。

数据

dat <- data.frame(
  Measurement = c(1, 1, 1, 1, 2, 2, 2, 2),
  Compound = c("A", "A", "B", "B", "C", "C", "D", 'D'),
  Measure = 111 * 1:8
)

像这样:

library(tidyverse)
dat <- data.frame(
  measument = c(1,1,2,2),
  compount = c(letters[1:4])
)

dat %>% group_by(measument) %>% 
  mutate(compount_order = 1:n())
#> # A tibble: 4 x 3
#> # Groups:   measument [2]
#>   measument compount compount_order
#>       <dbl> <chr>             <int>
#> 1         1 a                     1
#> 2         1 b                     2
#> 3         2 c                     1
#> 4         2 d                     2

reprex package (v2.0.0)

于 2021-11-05 创建

data.table解决方法

library(data.table)
setDT(dat)[, Compound_order := rleid(Compound), by = .(Measurement)]
#    Measurement Compound Measure Compound_order
# 1:           1        A     111              1
# 2:           1        A     222              1
# 3:           1        B     333              2
# 4:           1        B     444              2
# 5:           2        C     555              1
# 6:           2        C     666              1
# 7:           2        D     777              2
# 8:           2        D     888              2

您可以执行以下操作。

library(dplyr)

dat2 <- dat %>%
  group_by(Measurement) %>%
  mutate(Compound_order = as.integer(factor(Compound,
                                            levels = unique(Compound)))) %>%
  ungroup()

dat2
# # A tibble: 8 x 4
#   Measurement Compound Measure Compound_order
#         <dbl> <chr>      <dbl>          <int>
# 1           1 A            111              1
# 2           1 A            222              1
# 3           1 B            333              2
# 4           1 B            444              2
# 5           2 C            555              1
# 6           2 C            666              1
# 7           2 D            777              2
# 8           2 D            888              2

数据

dat <- data.frame(
  Measurement = c(1, 1, 1, 1, 2, 2, 2, 2),
  Compound = c("A", "A", "B", "B", "C", "C", "D", 'D'),
  Measure = 111 * 1:8
)