簡體   English   中英

dplyr中group_by層次結構內的計數級別

[英]Count level within group_by hierarchy in dplyr

我在R中有一個大數據集,該數據集由來自各個案例的多個記錄組成,嵌套在組中。 一個玩具的例子在這里:

d = data.frame(group = rep(c('control','patient'), each = 5), case = c('a', 'a', 'b', 'c', 'c', 'd','d','d','e','e'))

如果在dplyr鏈中應用了group_by(group, case) ,那么如何創建一列以行在組中的大小順序對每一行編號? 例如,在下面的示例中,在第三列中,病例“ a”是對照組中的第一個病例,病例“ c”是對照組中的第一個病例,但是對於病例“ d”(患者中的第一個病例),編號重置為1組。

  group case  number
control  a    1
control  a    1
control  b    2
control  c    3
control  c    3
patient  d    1
patient  d    1
patient  d    1
patient  e    2
patient  e    2

我可以看到通過使用“ for”循環對個案進行計數來了解如何做到這一點,但我想知道在標准的dplyr風格的操作鏈中是否有辦法實現這一目標?

group_by(d, group) %>% 
   mutate(number= droplevels(case) %>% as.numeric)

我們可以使用data.table

library(data.table)
setDT(d)[, numbers := as.numeric(factor(case, levels = unique(case))), group]

一種解決方案是:

library(dplyr)
library(tibble)

want<-left_join(d,
                d %>%
                  distinct(case) %>%
                  rownames_to_column(var="number") ,
                by="case")

# .. added later:
want2<-left_join(d,
                 bind_rows(
                   d %>%
                     filter(group=="control") %>%
                     distinct(case) %>%
                     rownames_to_column(var="number"),
                   d %>%
                     filter(group=="patient") %>%
                     distinct(case) %>%
                     rownames_to_column(var="number")),
                   by="case")

# I think this is less readable:
want3<-left_join(d,
                 bind_rows(by(d,d$group,function(x) x %>%
                                distinct(case) %>%
                                rownames_to_column(var="number"))),
                 by="case")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM