R - 根据另一个单元格值按组连接数据框中的单元格
R - Concatenate cell in dataframe, by group, depending on another cell value
我有以下类型的数据集(第一行是 header):
content
总是文本
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
问题:我需要按照一个非常具体的标准进行合并:
- 具有
merge = FALSE
的行必须保持原样
- 具有相同
id1
、相同 id2
和连续 start_line
的行:
- 需要追加到列
content
end_line
值需要更改到最后一行
因此,预期结果将是:
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
请注意示例中:
- 最小合并是两行(id示例:C-D,最初是第6行和第7行)
- 可以合并多行(id 示例 A-B,最初是第 2、3、4 行)
我尝试了一系列非常大且效率低下的循环,它们只合并了两行。这就是为什么我不在这里发布我的尝试。
使用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
数据
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))
我有以下类型的数据集(第一行是 header):
content
总是文本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
问题:我需要按照一个非常具体的标准进行合并:
- 具有
merge = FALSE
的行必须保持原样 - 具有相同
id1
、相同id2
和连续start_line
的行:- 需要追加到列
content
end_line
值需要更改到最后一行
- 需要追加到列
因此,预期结果将是:
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
请注意示例中:
- 最小合并是两行(id示例:C-D,最初是第6行和第7行)
- 可以合并多行(id 示例 A-B,最初是第 2、3、4 行)
我尝试了一系列非常大且效率低下的循环,它们只合并了两行。这就是为什么我不在这里发布我的尝试。
使用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
数据
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))