r - 如何合并一列中的行以匹配另一列中的非空行?

标签 r csv text

我有一个包含两列的 .csv 文件。第一个是 ID,第二个是文本字段。但是,文本字段中的文本被拆分成句子并延伸到另一行,因此文件如下所示:

ID TEXT
TXT_1 This is the first sentence
NA This is the second sentence
NA This is the third sentence
TXT_2 This is the first sentence of the second text
NA This is the second sentence of the second text

我想做的是合并文本字段,使其看起来像这样:

ID TEXT
TXT_1 This is the first sentence This is the second sentence This is the third sentence
TXT_2 This is the first sentence of the second text This is the second sentence of the second text

在 R 中有一个简单的解决方案吗?

最佳答案

我们根据“ID”中的非 NA 元素创建一个分组变量,并将“TEXT”粘贴在一起

library(dplyr)
df1 %>% 
    group_by(Grp = cumsum(!is.na(ID))) %>% 
    summarise(ID = ID[!is.na(ID)], TEXT = paste(TEXT, collapse = ' ')) %>%
    ungroup() %>%
    select(-Grp)  
# A tibble: 2 x 2
#     ID                                                                                         TEXT
#    <chr>                                                                                        <chr>
#1 TXT_1            This is the first sentence This is the second sentence This is the third sentence
#2 TXT_2 This is the first sentence of the second text This is the second sentence of the second text

或者按照@Jaap的建议

df1 %>% 
   group_by(ID = zoo::na.locf(ID)) %>%
   summarise(TEXT = paste(TEXT, collapse = ' ')) 

关于r - 如何合并一列中的行以匹配另一列中的非空行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44695235/

相关文章:

r - 使用 RCurl(或任何其他方法)从 FTP 下载 .RData 和 .csv 文件

r - 如何使用列名称中的数字创建列?

batch-file - 使用批处理文件列出 csv 上的文件名和文件夹路径

php - 在php中读取文本文件并使用其内容从mysql数据库中获取数据

mongodb - mongodb文本搜索错误没有文本索引

使用 aws.s3 包从 AWS S3 一次读取多个 CSV 文件对象

R shiny numericInput 步骤和最小值交互

excel - 当平面文件中的列和行不匹配时,如何解决(使用)ETL

python - 在 CSV 文件 python 中添加新行和现有迭代行

HTML/CSS 省略号 (...) 不工作