R 如何合并一列中的行以匹配另一列中的非空行?
我有一个包含两列的.csv文件。第一个是ID,第二个是文本字段。但是,文本字段中的文本被拆分成句子,并延伸到另一行,因此文件如下所示:R 如何合并一列中的行以匹配另一列中的非空行?,r,csv,text,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 senten
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
我想做的是合并文本字段,使其看起来像这样:
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元素创建一个分组变量,并将“文本”粘贴在一起
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
或者:df1%>%groupby(ID=zoo::na.locf(ID))%>%summary(TEXT=paste(TEXT,collapse='')