【问题标题】:How to assign a "reseting" group number by the second grouping variable in R?如何通过 R 中的第二个分组变量分配“重置”组号?
【发布时间】:2021-12-19 07:27:42
【问题描述】:

我的数据如下所示:

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

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

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
)

【问题讨论】:

  • 感谢您的良好开端。我原来的示例表有点过于简化了。现在添加了一列和几行以获得正确的想法。 ——
  • 每次测量可以有随机的行数,不一定是2*X + 2*Y。
  • 我已根据您的更新更新了我的答案。
  • 我通过提供 R 代码来创建您的示例数据框来更新您的问题。请注意,如果您下次能提供一个可重现的示例,那就太好了。
  • 当然,我会的。对于看到数据的人来说,有些东西自然会更明显。

标签: r data-manipulation data-preprocessing


【解决方案1】:

data.tablesolution

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

【讨论】:

  • 感谢您的精彩开始。我原来的示例表有点过于简化了。现在添加了一列和几行以获得正确的想法。
  • 根据新样本数据更新
【解决方案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
)

【讨论】:

    【解决方案3】:

    像这样:

    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 创建

    【讨论】:

    • 感谢您的精彩开始。我原来的示例表有点过于简化了。现在添加了一列和几行以获得正确的想法。 ——
    猜你喜欢
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2022-01-03
    • 2021-08-21
    • 1970-01-01
    相关资源
    最近更新 更多