% fill(id) %-6ren">
gpt4 book ai didi

R:如何连接分成多行的字符串?

转载 作者:行者123 更新时间:2023-12-02 20:36:02 33 4
gpt4 key购买 nike

我有一个如下所示的数据框:

df1 <- data.frame(Question=c("This is the start", "of a question", "This is a second", "question"), 
Answer = c("Yes", "", "No", ""))

Question Answer
1 This is the start Yes
2 of a question
3 This is a second No
4 question

这是虚拟数据,但真实数据是通过制表器从 PDF 中提取的。只要源文档中的 Question 出现换行符,该问题就会分成多行。如何根据 Answer 为空的条件串联回去?

想要的结果很简单:

                     Question     Answer
1 This is the start of a question Yes
2 This is a second question No

逻辑很简单,如果 Answer[x] 为空,则连接 Question[x]Question[x-1] 并删除行x

最佳答案

这无疑可以改进,但如果您乐意使用 tidyverse,也许这样的方法可以工作?

library(dplyr)
library(tidyr)
library(stringr)

df1 %>%
mutate(id = if_else(Answer != "", row_number(), NA_integer_)) %>%
fill(id) %>% group_by(id) %>%
summarise(Question = str_c(Question, collapse = " "), Answer = first(Answer))

#> # A tibble: 2 x 3
#> id Question Answer
#> <int> <chr> <fctr>
#> 1 1 This is the start of a question Yes
#> 2 3 This is a second question No

关于R:如何连接分成多行的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47147657/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com