Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何合并一列中的行以匹配另一列中的非空行?_R_Csv_Text - Fatal编程技术网

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

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

我有一个包含两列的.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
我想做的是合并文本字段,使其看起来像这样:

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='')