【问题标题】:Convert several columns to unique one and match it with other columns information将几列转换为唯一的一列并将其与其他列信息匹配
【发布时间】:2017-07-03 12:58:15
【问题描述】:

我正在处理一个复杂的矩阵(对我来说很复杂......)

是这样的:

      Invoice.1   Invoice.2   Invoice.3               mtime
1   21605000182 21605000183          NA 2017-01-16 19:51:33
2   21605000182 21605000183          NA 2017-01-16 19:51:33
3   21605000182 21605000183          NA 2017-01-16 19:51:33
4   21605000182 21605000183          NA 2017-01-16 19:51:33
5   21510000669 21602000125 21608000366 2017-01-20 13:28:36
6   21609000856          NA          NA 2017-01-20 13:28:36
7   21606000405 21608000354 21608000356 2017-01-20 13:28:36
8   21610000133          NA          NA 2017-01-20 13:28:36
9   21604000592 21605000604 21605000608 2017-01-20 13:28:36
10  21609001012          NA          NA 2017-01-20 13:28:36

我想将所有 Invoice 列转换为一个,以清理“NA”和重复项,但要尊重每个列与最后一列日期的匹配,即索赔日期。

类似的东西:

      Invoice          mtime
1   21605000182 2017-01-16 19:51:33
2   21605000182 2017-01-16 19:51:33
3   21605000182 2017-01-16 19:51:33
4   21605000182 2017-01-16 19:51:33
5   21510000669 2017-01-20 13:28:36
6   21609000856 2017-01-20 13:28:36
7   21606000405 2017-01-20 13:28:36
8   21610000133 2017-01-20 13:28:36
9   21604000592 2017-01-20 13:28:36
10  21609001012 2017-01-20 13:28:36
11  21605000183 2017-01-16 19:51:33
12  21605000183 2017-01-16 19:51:33
13  21605000183 2017-01-16 19:51:33
14  21605000183 2017-01-16 19:51:33
15  21602000125 2017-01-20 13:28:36
16  21608000354 2017-01-20 13:28:36

【问题讨论】:

  • unique(df) 将删除初始 data.frame 中的重复行。那你要重塑长:stackoverflow.com/questions/2185252/…
  • 因为 Imo 已经建议使用类似:library(reshape2); melt(data)library(tidyverse); data %>% gather(key, value, -mtime)
  • 我认为你可以在 tidyr 库中使用一些东西,比如 data % gather(tmp, Invoice, c(Invoice.1,Invoice.2,Invoice.3))
  • @lmo 在应用唯一之前有必要重塑矩阵。 I. 尝试应用数据 %>% 收集...现在无法解决
  • 在您的示例中,第 1 行是第 2 行的副本。在这种情况下,最初使用 unique 将减少重塑算法的负担,并可能显着加快该过程。重塑后,您必须再次使用unique。我提供的链接中有很多解决方案。

标签: r match multiple-columns data-cleaning


【解决方案1】:

使用data.table 的示例:(应该比使用其他称呼更快)

DT <- data.table(Invoice.1 = 1:3, Invoice.2 = c(1L,4L,5L), mtime = 11:13)
DT

   Invoice.1 Invoice.2 mtime
1:         1         1    11
2:         2         4    12
3:         3         5    13

rez <- melt(DT, measure.vars = paste0("Invoice.", 1:2),
            value.name = "Invoice")
rez[, variable := NULL]
rez

   mtime Invoice
1:    11       1
2:    12       2
3:    13       3
4:    11       1
5:    12       4
6:    13       5

rez <- unique(rez)
rez

   mtime Invoice
1:    11       1
2:    12       2
3:    13       3
4:    12       4
5:    13       5

【讨论】:

    【解决方案2】:

    tidyr 包使用gather 函数可以满足您的需求。 gather 会将data.frame 从宽格式转换为长格式。

    library(tidyr)
    library(readr)
    
    # Create a temp file to store the example data
    data_file <- tempfile()
    
    cat(
    "Invoice.1,Invoice.2,Invoice.3,mtime
    21605000182,21605000183,NA,2017-01-16 19:51:33
    21605000182,21605000183,NA,2017-01-16 19:51:33
    21605000182,21605000183,NA,2017-01-16 19:51:33
    21605000182,21605000183,NA,2017-01-16 19:51:33
    21510000669,21602000125,21608000366,2017-01-20 13:28:36
    21609000856,NA,NA,2017-01-20 13:28:36
    21606000405,21608000354,21608000356,2017-01-20 13:28:36
    21610000133,NA,NA,2017-01-20 13:28:36
    21604000592,21605000604,21605000608,2017-01-20 13:28:36
    21609001012,NA,NA,2017-01-20 13:28:36",
    file = data_file,
    append = FALSE)
    
    # Read the data from the temp file into a data.frame called `invoices`
    invoices <-
      readr::read_csv(file = data_file, col_types = "cccT")
    
    # View the data
    invoices
    # # A tibble: 10 x 4
    #      Invoice.1   Invoice.2   Invoice.3               mtime
    #          <chr>       <chr>       <chr>              <dttm>
    #  1 21605000182 21605000183        <NA> 2017-01-16 19:51:33
    #  2 21605000182 21605000183        <NA> 2017-01-16 19:51:33
    #  3 21605000182 21605000183        <NA> 2017-01-16 19:51:33
    #  4 21605000182 21605000183        <NA> 2017-01-16 19:51:33
    #  5 21510000669 21602000125 21608000366 2017-01-20 13:28:36
    #  6 21609000856        <NA>        <NA> 2017-01-20 13:28:36
    #  7 21606000405 21608000354 21608000356 2017-01-20 13:28:36
    #  8 21610000133        <NA>        <NA> 2017-01-20 13:28:36
    #  9 21604000592 21605000604 21605000608 2017-01-20 13:28:36
    # 10 21609001012        <NA>        <NA> 2017-01-20 13:28:36
    
    # use the gather function from the tidyr package to transform the data from the
    # wide format to a long format.
    
    tidyr::gather(invoices, key = key, value = Invoice, -mtime, na.rm = TRUE) %>% print(n = Inf)
    # # A tibble: 20 x 3
    #                  mtime       key     Invoice
    #  *              <dttm>     <chr>       <chr>
    #  1 2017-01-16 19:51:33 Invoice.1 21605000182
    #  2 2017-01-16 19:51:33 Invoice.1 21605000182
    #  3 2017-01-16 19:51:33 Invoice.1 21605000182
    #  4 2017-01-16 19:51:33 Invoice.1 21605000182
    #  5 2017-01-20 13:28:36 Invoice.1 21510000669
    #  6 2017-01-20 13:28:36 Invoice.1 21609000856
    #  7 2017-01-20 13:28:36 Invoice.1 21606000405
    #  8 2017-01-20 13:28:36 Invoice.1 21610000133
    #  9 2017-01-20 13:28:36 Invoice.1 21604000592
    # 10 2017-01-20 13:28:36 Invoice.1 21609001012
    # 11 2017-01-16 19:51:33 Invoice.2 21605000183
    # 12 2017-01-16 19:51:33 Invoice.2 21605000183
    # 13 2017-01-16 19:51:33 Invoice.2 21605000183
    # 14 2017-01-16 19:51:33 Invoice.2 21605000183
    # 15 2017-01-20 13:28:36 Invoice.2 21602000125
    # 16 2017-01-20 13:28:36 Invoice.2 21608000354
    # 17 2017-01-20 13:28:36 Invoice.2 21605000604
    # 18 2017-01-20 13:28:36 Invoice.3 21608000366
    # 19 2017-01-20 13:28:36 Invoice.3 21608000356
    # 20 2017-01-20 13:28:36 Invoice.3 21605000608
    

    【讨论】:

      猜你喜欢
      • 2021-06-07
      • 2018-10-11
      • 1970-01-01
      • 2013-06-20
      • 2020-08-06
      • 2015-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多