【问题标题】:Aggregate and collapse a vector based while maintaing order在保持顺序的同时聚合和折叠基于向量
【发布时间】:2020-05-15 15:51:14
【问题描述】:

我有一个如下的数据框:

+------+-----+----------+
| from | to  | priority |
+------+-----+----------+
|    1 |   8 |        1 |
|    2 |   6 |        1 |
|    3 |   4 |        1 |
|    4 |   5 |        3 |
|    5 |   6 |        4 |
|    6 |   2 |        5 |
|    7 |   8 |        2 |
|    4 |   3 |        5 |
|    2 |   1 |        1 |
|    6 |   6 |        4 |
|    1 |   7 |        5 |
|    8 |   4 |        6 |
|    9 |   5 |        3 |
+------+-----+----------+

我的目标是根据 from 列对“to”列进行分组,但如果变量已经存在于任一列中,我不想进一步考虑它们 此外,总优先级将是所有组优先级的总和

所以生成的数据框如下:

+------+------+----------------+
| from |  to  | Total Priority |
+------+------+----------------+
|    1 | 8, 7 |              6 |
|    2 |    6 |              1 |
|    3 |    4 |              1 |
|    9 |    5 |              3 |
+------+------+----------------+

另外,我希望在分组时保持与原始表格相同的顺序

我能够使用下面的“splitstackshape”包折叠从列

library(splitstackshape)
cSplit(df, 'to', sep = ','
+        , direction = 'long')[, .(to = toString(unique(to)))
+                              , by = from]

这确实引入了重复值 我想知道是否有办法使用任何其他软件包获得所需的结果

【问题讨论】:

  • 组是如何创建的?你能解释一下你的预期输出吗?我也不认为cSplit 在这里做任何事情。您的数据已经是长格式。

标签: r dataframe dplyr plyr


【解决方案1】:

目前尚不清楚您究竟是如何尝试创建组的,但这至少会让您进入正确的范围:

library(tidyverse)

df <- tribble(~from, ~to, ~priority,
              1,8,1,
              2,6,1,
              3,4,1,
              4,5,3,
              5,6,4,
              6,2,5,
              7,8,2,
              4,3,5,
              2,1,1,
              6,6,4,
              1,7,5,
              8,4,6,
              9,5,3)

df %>%
  group_by(from) %>%
  summarise(to = toString(to),
            `Total Priority` = sum(priority, na.rm=T))

你的结果是:

# A tibble: 9 x 3
   from to    `Total Priority`
  <dbl> <chr>            <dbl>
1     1 8, 7                 6
2     2 6, 1                 2
3     3 4                    1
4     4 5, 3                 8
5     5 6                    4
6     6 2, 6                 9
7     7 8                    2
8     8 4                    6
9     9 5                    3

【讨论】:

    【解决方案2】:

    使用DF 在最后的注释中重复显示,按from 排序,给出DF2,然后遍历其行,删除任何重复的行。我们在这里需要一个循环,因为每次删除都取决于先前的删除。最后总结一下结果。

    library(dplyr)
    
    DF2 <- arrange(DF, from)
    
    i <- 1
    while(i <= nrow(DF2)) {
      ix <- seq_len(i-1)
      dup <- with(DF2, (to[i] %in% c(to[ix], from[ix])) | (from[i] %in% to[ix]))
      if (dup) DF2 <- DF2[-i, ] else i <- i + 1
    }
    
    DF2 %>%
      group_by(from) %>%
      summarize(to = toString(to), priority = sum(priority)) %>%
      ungroup
    

    给予:

    # A tibble: 4 x 3
       from to    priority
      <int> <chr>    <int>
    1     1 8, 7         6
    2     2 6            1
    3     3 4            1
    4     9 5            3
    

    注意

    Lines <- "from | to  | priority
       1 |   8 |        1
       2 |   6 |        1
       3 |   4 |        1
       4 |   5 |        3
       5 |   6 |        4
       6 |   2 |        5
       7 |   8 |        2
       4 |   3 |        5
       2 |   1 |        1
       6 |   6 |        4
       1 |   7 |        5
       8 |   4 |        6
       9 |   5 |        3"
    DF <- read.table(text = Lines, header = TRUE, sep = "|", strip.white = TRUE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-31
      • 2017-06-25
      • 1970-01-01
      • 2021-11-22
      • 2016-09-20
      • 1970-01-01
      • 2012-08-05
      • 1970-01-01
      相关资源
      最近更新 更多