简体   繁体   English

R - 连接 dataframe 中的单元格,按组,取决于另一个单元格值

[英]R - Concatenate cell in dataframe, by group, depending on another cell value

I have a dataset of the following type (first row is the header):我有以下类型的数据集(第一行是标题):

  • content is always text content始终是文本
  • merge is always a logical merge总是合乎逻辑的
id1  id2  start_line end_line content           merge
A    B    1          1        "aaaa"            TRUE
A    B    4          4        "aa mm"           TRUE
A    B    5          5        "boool"           TRUE
A    B    6          6        "omw"             TRUE
C    D    6          6        "hear!"           TRUE
C    D    7          7        " me out!"        TRUE
C    D    21         21       "hello"           FALSE

Problem: I need to merge following a very specific criteria:问题:我需要按照一个非常具体的标准进行合并:

  • Rows that have merge = FALSE must remain as is具有merge = FALSE的行必须保持原样
  • Rows that have: same id1 , same id2 and consecutive start_line :具有相同id1 、相同id2和连续start_line的行:
    • Need to be appended on the column content需要附加在栏目content
    • The end_line value needs to change to the last row end_line值需要更改为最后一行

So, the expected result would be:所以,预期的结果是:

id1  id2  start_line end_line content             merge
A    B    1          1        "aaaa"              TRUE
A    B    4          6        "aa mm boool omw"   TRUE
C    D    6          7        "hear!  me out!"    TRUE
C    D    21         21       "hello"             FALSE

Notice in the example that:请注意示例中的:

  • The minimal merge is with two lines (example of ids: CD, originally 6th and 7th rows)最小合并是两行(ID 示例:CD,最初是第 6 行和第 7 行)
  • There can be multiple lines to merge (example of ids AB, originally rows 2nd, 3rd, 4th)可以合并多行(ids AB 的示例,最初是第 2、3、4 行)

I have attempted a very large, and inefficient series of loops, that only merge two lines.我尝试了一个非常大且效率低下的循环系列,只合并两行。 That is why I am not posting my attempt here.这就是为什么我没有在这里发布我的尝试。

Using dplyr you can try:使用dplyr您可以尝试:

library(dplyr)

df %>%
 group_by(id1, id2, grp = cumsum(c(TRUE, diff(start_line) > 1))) %>%
 summarise(start_line = first(start_line), 
           end_line = last(end_line), 
           content = paste(content, collapse = " "), 
            merge = any(merge))


#  id1   id2     grp start_line end_line content         merge
#  <chr> <chr> <int>      <int>    <int> <chr>           <lgl>
#1 A     B         1          1        1 aaaa            TRUE 
#2 A     B         2          4        6 aa mm boool omw TRUE 
#3 C     D         2          6        7 hear!  me out!  TRUE 
#4 C     D         3         21       21 hello           FALSE

data数据

df <- structure(list(id1 = c("A", "A", "A", "A", "C", "C", "C"), id2 = c("B", 
"B", "B", "B", "D", "D", "D"), start_line = c(1L, 4L, 5L, 6L, 
6L, 7L, 21L), end_line = c(1L, 4L, 5L, 6L, 6L, 7L, 21L), content = c("aaaa", 
"aa mm", "boool", "omw", "hear!", " me out!", "hello"), merge = c(TRUE, 
TRUE, TRUE, TRUE, TRUE, TRUE, FALSE)), class = "data.frame", 
row.names = c(NA, -7L))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM