【问题标题】:How to create a column that records how many times a person_id occur?如何创建一个记录person_id出现次数的列?
【发布时间】:2022-12-03 06:03:58
【问题描述】:

我想创建一个名为 visit_occurrance 的列,用于汇总每个 person_id 在数据集中重新出现的次数。例如,

> dput(df)
structure(list(Person_ID = c(123L, 123L, 110L, 145L, 345L, 345L, 
345L, 345L, 300L, 234L, 234L, 111L, 110L)), class = "data.frame", row.names = c(NA, 
-13L))

期望的输出:

> dput(df)
structure(list(Person_ID = c(123L, 123L, 110L, 145L, 345L, 345L, 
345L, 345L, 300L, 234L, 234L, 111L, 110L), Visit_occurrance = c(1L, 
2L, 1L, 1L, 1L, 2L, 3L, 4L, 1L, 1L, 2L, 1L, 2L)), class = "data.frame", row.names = c(NA, 
-13L))

【问题讨论】:

    标签: r


    【解决方案1】:
    library(dplyr)
                                                                                                                 -13L))
    df %>% 
      group_by(Person_ID) %>% 
      mutate(Visit_occurrance = row_number())
    
       Person_ID Visit_occurrance
           <int>            <int>
     1       123                1
     2       123                2
     3       110                1
     4       145                1
     5       345                1
     6       345                2
     7       345                3
     8       345                4
     9       300                1
    10       234                1
    11       234                2
    12       111                1
    13       110                2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-21
      • 2020-12-03
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-16
      • 2016-07-27
      相关资源
      最近更新 更多