【问题标题】:categorizing Types in bucket using R使用 R 对存储桶中的类型进行分类
【发布时间】:2019-09-23 21:31:41
【问题描述】:

我有一个如下所示的数据框:

DF1

ID      Value      Type         Date
II-1    150        Active       2019-01-01 15:34:18
II-1    175        Active       2019-01-01 15:34:18
II-1    165        Active       2019-01-01 15:34:18
II-1    168        Active       2019-01-01 15:34:18
II-2    200        InActive     2019-01-05 17:14:20
II-2    45         InActive     2019-01-05 17:14:20
II-3    34         InActive     2019-02-04 11:04:12
II-4    78         InActive     2019-02-01 12:33:14

我需要在R中将上面的输出转换成下面提到的格式来实现下面提到的格式。

其中,1-33-5 是根据Type 在其所属的桶上的 ID 计数的分叉。

示例:如果 ID II-1 出现四次,则它将落入 3-5 存储桶中,同样 ID II-2 将落入 1-3 存储桶中。

Month       Total      Active  1-3   3-5   InActive   1-3   3-5  Hold  1-3  3-5
Jan-19       6           2      1     1       0         0    0     0    0    0
Feb-19       2           0      0     0       2         2    0     0    0    0

【问题讨论】:

  • 您是从数据库还是从内存数据中获取这些信息?
  • @mj_whales:是的,我从同一个数据库但不同的表中获取这些信息,并且我已经创建了查询输出的数据框。
  • 你在使用 SQL Server 吗?
  • @mj_whales: MySQL 使用库 'RMySQL'
  • 所以澄清一下,您已经知道如何连接到数据库并获取数据,但您现在需要做的就是将已有的数据操作到示例输出中。这是正确的吗?

标签: r dataframe dplyr


【解决方案1】:

如果您也想要总数,您可以单独找到这些并进行连接:

## Libraries
library(tidyverse)
library(lubridate)


## Alter the DF1 table to get months in the right format: DF1_new
DF1_new <- DF1 %>%
  # Create new month column
  mutate(Month = as_factor(str_c(month(Date, label = TRUE), year(Date), sep = "-")),
         Type = as_factor(Type)) %>%
  # Reorder columns
  select(Month, everything())


## Group DF1_new by Month and Type: right
right <- DF1_new %>%
  # Count ID by month and type
  count(Month, Type, ID) %>%
  # Place each in buckets by count
  mutate(Bucket = case_when(n < 4 ~ "1-3", TRUE ~ "4-5")) %>%
  # Combine bucket names
  unite(Type.Bucket, c(Type, Bucket), sep = ".") %>%
  # Count how many IDs fall in each bucket type
  count(Month, Type.Bucket) %>%
  spread(Type.Bucket, n)


## Get month totals and join to month/type data frame
DF1_new %>%
  # Count ID by month
  group_by(Month) %>%
  summarise(Total = n()) %>%
  left_join(right, by = "Month")

结果:

Month       Total    Active.4-5    InActive.1-3
Jan-2019      6        1              1 
Feb-2019      2        NA             2 

【讨论】:

    【解决方案2】:

    这是一个粗略的方法,可以调整以获得您想要的特定列。

    library(lubridate); library(tidyverse)
    DF1 %>%
      count(Month = floor_date(Date, "month"), Type, ID) %>%
      mutate(bucket = case_when(n < 4 ~ "1-3", TRUE  ~ "4-5")) %>%
      count(Month, Type, bucket) %>%
      unite(column, Type:bucket) %>%
      spread(column, n, fill = 0)
    
    ## A tibble: 2 x 3
    #  Month               `Active_4-5` `InActive_1-3`
    #  <dttm>                     <dbl>          <dbl>
    #1 2019-01-01 00:00:00            1              1
    #2 2019-02-01 00:00:00            0              2
    

    数据:

    DF1 <- structure(list(ID = c("II-1", "II-1", "II-1", "II-1", "II-2", 
    "II-2", "II-3", "II-4"), Value = c(150L, 175L, 165L, 168L, 200L, 
    45L, 34L, 78L), Type = c("Active", "Active", "Active", "Active", 
    "InActive", "InActive", "InActive", "InActive"), Date = structure(c(1546385658, 
    1546385658, 1546385658, 1546385658, 1546737260, 1546737260, 1549307052, 
    1549053194), class = c("POSIXct", "POSIXt"), tzone = "")), row.names = c(NA, 
    -8L), class = "data.frame")
    

    【讨论】:

    • 收到错误Error in count(., Month = floor_date(Date, "month"), Type, ID) : unused argument (Month = floor_date(Date, "month"))
    • 添加了我从 OP 使用的 DF1 版本并将日期转换为 POSIXct。使用它作为数据是否会出现相同的错误?你能加载 lubridate(floor_date 函数的来源)吗?
    猜你喜欢
    • 1970-01-01
    • 2017-10-23
    • 2020-02-03
    • 2022-01-18
    • 2016-05-09
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 2017-11-29
    相关资源
    最近更新 更多