【问题标题】:Match two column value in entire dataset [Specific Case]匹配整个数据集中的两列值 [具体案例]
【发布时间】:2020-11-25 17:42:08
【问题描述】:

我在 R 中有下面提到的两个数据框,我尝试了各种方法但仍然无法达到所需的输出。

DF:

ID     Date                 city        code    uid
I-1    2020-01-01 10:12:15  New York     123    K-1
I-1    2020-01-01 10:12:15  Utha         103    K-1
I-2    2020-01-02 10:12:15  Washington   122    K-1
I-3    2020-02-01 10:12:15  Tokyo        123    K-2
I-3    2020-02-01 10:12:15  Osaka        193    K-2
I-4    2020-02-02 10:12:15  London       144    K-3
I-5    2020-02-04 10:12:15  Dubai        101    K-4
I-6    2019-11-01 10:12:15  Dubai        101    K-4
I-7    2019-11-01 10:12:15  London       144    K-3
I-8    2018-12-13 10:12:15  Tokyo        143    K-5
I-9    2019-05-17 10:12:15  Dubai        101    K-4
I-19   2020-03-11 10:12:15  Dubai        150    K-7

输入:

structure(list(ID = c("I-1", "I-1", 
"I-2", "I-3", "I-3", "I-4", 
"I-5", "I-6", "I-7", "I-8", "I-9","I-19" 
), DATE = c("2020-01-01 11:49:40.842", "2020-01-01 09:35:33.607", 
"2020-01-02 06:14:58.731", "2020-02-01 16:51:27.190", "2020-02-01 05:35:46.952", 
"2020-02-02 05:48:49.443", "2020-02-04 10:00:41.616", "2019-11-01 09:10:46.536", 
"2019-11-01 11:54:05.655", "2018-12-13 14:24:31.617", "2019-05-17 14:24:31.617", "2020-03-11 14:24:31.617"), CITY = c("New York", 
"UTAH", "Washington", "Tokyo", 
"Osaka", "London", "Dubai", 
"Dubai", "London", "Tokyo", "Dubai", 
"Dubai"), CODE = c("221010", 
"411017", "638007", "583101", "560029", "643102", "363001", "452001", 
"560024", "509208"), UID = c("K-1", 
"K-1", "K-1", "K-2", "K-2", 
"K-3", "K-4", "K-4", "K-3", 
"K-5","K-4","K-7")), .Names = c("ID", "DATE", 
"CITY", "CODE", "UID"), row.names = c(NA, 
10L), class = "data.fram)

使用上述两个数据框,我想获取 2020 年 1 月 1 日至 2002 年 2 月 29 日之间的记录,并比较整个数据库中的这些 ID,以检查城市和代码是否与其他 ID 匹配并进一步分类以检查如何许多有相同的 uid,有多少有不同。

在哪里,

  • 匹配 - 城市和代码的组合与数据库中的其他 ID 匹配
  • Same_uid - 对匹配 ID 进行分类,以确定有多少 ID 具有相似的 uid
  • different_uid - 对匹配 id 进行分类以识别有多少 ID 没有相似的 uid
  • uid_count - 整个数据库中该特定 ID 的相似 uid 计数
  • Match_with - 这基本上是为了让那些ID 同时具有citycode 与特定的ID 相同

注意 - 我在数据框中有超过 1000 万条记录。

需要的输出

    ID      Date                 city        code  uid  Match   Same_uid   different_uid uid_count Match_with
    I-1     2020-01-01 10:12:15  New York    123   K-1  No      0          0              2        NA
    I-2     2020-01-02 10:12:15  Washington  122   K-1  No      0          0              2        NA
    I-3     2020-02-01 10:12:15  Tokyo       123   K-2  No      0          0              1        NA
    I-4     2020-02-02 10:12:15  London      144   K-3  Yes     1          0              2        I-7
    I-5     2020-02-04 10:12:15  Dubai       101   K-4  Yes     2          0              3        I-6, I-9

【问题讨论】:

    标签: r dataframe dplyr tidyverse


    【解决方案1】:

    这只是一些开端,我仍然没有得到你需要的所有东西,也许我有什么问题,我们可以进一步讨论

    library(tidyverse)
    library(lubridate)
    #> 
    #> Attaching package: 'lubridate'
    #> The following objects are masked from 'package:base':
    #> 
    #>     date, intersect, setdiff, union
    
    df_example <- read_table("ID     Date                 city        code    uid
    I-1    2020-01-01 10:12:15  New York     123    K-1
    I-1    2020-01-01 10:12:15  Utha         103    K-1
    I-2    2020-01-02 10:12:15  Washington   122    K-1
    I-3    2020-02-01 10:12:15  Tokyo        123    K-2
    I-3    2020-02-01 10:12:15  Osaka        193    K-2
    I-4    2020-02-02 10:12:15  London       144    K-3
    I-5    2020-02-04 10:12:15  Dubai        101    K-4
    I-6    2019-11-01 10:12:15  Dubai        101    K-4
    I-7    2019-11-01 10:12:15  London       144    K-3
    I-8    2018-12-13 10:12:15  Tokyo        143    K-5
    I-9    2019-05-17 10:12:15  Dubai        101    K-4
    I-19   2020-03-11 10:12:15  Dubai        150    K-7")
    #> Warning: Missing column names filled in: 'X3' [3]
    
    
    
    df_example1 <- df_example %>%
      unite(Date:X3,col = "Date",sep = " ") %>% 
      mutate(Date = Date %>% ymd_hms() %>% as_date())
    
    
    df_example1 %>%
      group_by(city,code) %>% 
      mutate(Match_with = toString(ID),
             different_uid = if_else(n_distinct(uid) - 1 == 0,0L,n()),
             uid_count = if_else(n_distinct(uid) - 1 == 0,n(),0L),
             same_uid = uid_count - 1)
    #> # A tibble: 12 x 9
    #> # Groups:   city, code [9]
    #>    ID    Date       city   code uid   Match_with different_uid uid_count
    #>    <chr> <date>     <chr> <dbl> <chr> <chr>              <int>     <int>
    #>  1 I-1   2020-01-01 New ~   123 K-1   I-1                    0         1
    #>  2 I-1   2020-01-01 Utha    103 K-1   I-1                    0         1
    #>  3 I-2   2020-01-02 Wash~   122 K-1   I-2                    0         1
    #>  4 I-3   2020-02-01 Tokyo   123 K-2   I-3                    0         1
    #>  5 I-3   2020-02-01 Osaka   193 K-2   I-3                    0         1
    #>  6 I-4   2020-02-02 Lond~   144 K-3   I-4, I-7               0         2
    #>  7 I-5   2020-02-04 Dubai   101 K-4   I-5, I-6,~             0         3
    #>  8 I-6   2019-11-01 Dubai   101 K-4   I-5, I-6,~             0         3
    #>  9 I-7   2019-11-01 Lond~   144 K-3   I-4, I-7               0         2
    #> 10 I-8   2018-12-13 Tokyo   143 K-5   I-8                    0         1
    #> 11 I-9   2019-05-17 Dubai   101 K-4   I-5, I-6,~             0         3
    #> 12 I-19  2020-03-11 Dubai   150 K-7   I-19                   0         1
    #> # ... with 1 more variable: same_uid <dbl>
    

    reprex package (v0.3.0) 于 2020-08-05 创建

    【讨论】:

    • 我想检查并比较某些特定日期范围ID 与整个日期集。例如,假设我们有 5 年的日期集,包含 100 万条记录,我想检查 ID May-20 到 Jul-20(50,000 条记录)与整个数据集进行检查和比较。
    • different_uid 列不起作用。此外,Match_with 仅包含匹配的 ID,不包括父 ID。例如,I-4I-7 匹配,所以我们只需要显示 I-7 而不是 I-4,I-7
    • 另外,Match 列未出现(即YesNo)。
    猜你喜欢
    • 2020-11-25
    • 1970-01-01
    • 2017-04-25
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多