【问题标题】:Finding most common value seperated by day查找按天分隔的最常见值
【发布时间】:2019-11-13 14:33:27
【问题描述】:

我想查看每个参与者每天最常出现的类别。每天都会出现多个类别,我想要一个新列来说明特定参与者在该特定日期最常出现的类别。

我有一列'user_id'、'date'和一列'category'(字符)。我应该使用哪个代码来添加一个新列,该列仅说明特定用户在特定日期出现最多的类别?

输入:

structure(list(user_id = c("10257", "10580", "10280", "10202", "10275","10281"),
date = structure(c(1552521600, 1552003200, 1551139200,1551484800, 1552867200, 1552521600), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
better_category = c("Email", "Internet_Browser", "Instant_Messaging","News","Background_Process","Instant_Messaging")),
row.nams = c(176300L, 184332L, 469288L, 119462L, 112507L, 399236L), 
class = "data.frame")

【问题讨论】:

  • 您能否使用dput 提供一些示例数据,以便我们尝试测试潜在的解决方案?
  • @iod 我附上了一张数据格式的图片。下面的代码确实给了我最常出现的类别(第 4 列“max”),但它给出了整个数据集最常见的类别,而不仅仅是每个特定用户每天最常见的值。你知道我该如何解决这个问题吗?
  • 不要将数据共享为图像。致电dput(head(data)) 分享您的一些数据并将其粘贴到您的问题中。
  • 请参阅下面的修订答案,其中包含虚假数据。我没有看到您描述的行为。
  • @iod 我认为这确实是问题所在。修改后的代码正在运行!非常感谢!!

标签: r date categorical-data data-processing topmost


【解决方案1】:

让我们创建一些数据:

require(dplyr)
set.seed(100)
data<-data.frame(user_id=rep(c(1,2,3),10),date=rep(c("tuesday","wednesday","thursday"),each=10),category=(sample(c(1:3),30,replace=TRUE)))

如果我们arrange方便查看,我们可以得到这个:

    data<-data %>% arrange(user_id,date)
    data
       user_id      date category
    1        1  thursday        3
    2        1  thursday        2
    3        1  thursday        3
    4        1   tuesday        1
    5        1   tuesday        1
    6        1   tuesday        3
    7        1   tuesday        1
    8        1 wednesday        1
    9        1 wednesday        3
    10       1 wednesday        2
    11       2  thursday        2
    12       2  thursday        1
    13       2  thursday        2
    14       2   tuesday        1
    15       2   tuesday        2
    16       2   tuesday        2
    17       2 wednesday        2
    18       2 wednesday        2
    19       2 wednesday        1
    20       2 wednesday        3
    21       3  thursday        2
    22       3  thursday        3
    23       3  thursday        3
    24       3  thursday        1
    25       3   tuesday        2
    26       3   tuesday        2
    27       3   tuesday        2
    28       3 wednesday        3
    29       3 wednesday        3
    30       3 wednesday        2

现在我们将按 user_id 和日期对其进行分组,并创建一个名为 max 的新列,该列从每个组中获取最常见的类别。我们在 `category 上使用 table 来执行此操作,这会为每个分组创建列的交叉表:

    data %>% group_by(user_id,date) %>% 
      dplyr::mutate(max=names(sort(table(category),decreasing=TRUE))[1])

# A tibble: 30 x 4
# Groups:   user_id, date [9]
   user_id date      category max  
     <dbl> <fct>        <int> <chr>
 1       1 thursday         3 3    
 2       1 thursday         2 3    
 3       1 thursday         3 3    
 4       1 tuesday          1 1    
 5       1 tuesday          1 1    
 6       1 tuesday          3 1    
 7       1 tuesday          1 1    
 8       1 wednesday        1 1    
 9       1 wednesday        3 1    
10       1 wednesday        2 1    
# ... with 20 more rows

如您所见,每个用户日分组都有自己的max。在她展示的最后一个示例中(1-星期三),三个类别各有一个,因此选择第一个,即 1。

这是使用您的 dput 数据的结果(其中每一行都有唯一的用户/日期配对):

# A tibble: 6 x 4
# Groups:   user_id, date [6]
  user_id date                better_category    max               
  <fct>   <dttm>              <fct>              <chr>             
1 10257   2019-03-14 00:00:00 Email              Email             
2 10580   2019-03-08 00:00:00 Internet_Browser   Internet_Browser  
3 10280   2019-02-26 00:00:00 Instant_Messaging  Instant_Messaging 
4 10202   2019-03-02 00:00:00 News               News              
5 10275   2019-03-18 00:00:00 Background_Process Background_Process
6 10281   2019-03-14 00:00:00 Instant_Messaging  Instant_Messaging 

所以我创建了一个相同的表,但将最后一行复制了两次,然后将其中一个类别更改为“新闻”,并运行相同的代码:

# A tibble: 8 x 4
# Groups:   user_id, date [6]
  user_id date                better_category    max               
  <chr>   <dttm>              <chr>              <chr>             
1 10257   2019-03-14 00:00:00 Email              Email             
2 10580   2019-03-08 00:00:00 Internet_Browser   Internet_Browser  
3 10280   2019-02-26 00:00:00 Instant_Messaging  Instant_Messaging 
4 10202   2019-03-02 00:00:00 News               News              
5 10275   2019-03-18 00:00:00 Background_Process Background_Process
6 10281   2019-03-14 00:00:00 News               Instant_Messaging 
7 10281   2019-03-14 00:00:00 Instant_Messaging  Instant_Messaging 
8 10281   2019-03-14 00:00:00 Instant_Messaging  Instant_Messaging 

注意最后三行。

【讨论】:

  • 仅代码的答案被认为是低质量的:请务必解释您的代码的作用以及它如何解决问题。
猜你喜欢
  • 2015-06-20
  • 1970-01-01
  • 1970-01-01
  • 2012-08-27
  • 2013-09-05
  • 2013-06-26
  • 2018-06-13
  • 1970-01-01
相关资源
最近更新 更多