r - 使用 R,将可变行数中的文本组合成单个文本元素

标签 r

什么 R 代码会将下面模拟数据框中每个人的叙述条目组合成一个变量?数据来自 Excel 电子表格,其中记录的叙述条目可以有 1 到 8 行。每个计时员的记录以一个空行结束。

假设这个数据框,dput() 如下:

> df
        timekeeper                                       narrative
1         Person A     Review and revise insert for audit response
2  Invoice=2858502 letter regarding separate investigation; review
3             <NA>            and exchange messages regarding same
4             <NA>                                            <NA>
5         Person B   Telephone conference with team; review e-mail
6  Invoice=2835951                 correspondence from X regarding
7             <NA>     credentialing issues; e-mail correspondence
8             <NA>               with Y regarding same; review and
9             <NA> approve transmittal letter for incident reports
10            <NA>                                            <NA>
11        Person C                  Telephone conference with X, Y
12 Invoice=2835951                     et al., regarding notice of
13            <NA>                                            <NA>
14        Person D                       Telephone conference with
15 Invoice=2835951    Brady, Gibson, et al., regarding DAB status;
16            <NA>            telephone conference with X, et al.,
17            <NA>    regarding physician investigation at 123 and
18            <NA>          medical liability insurance; telephone
19            <NA>                                            <NA>
20        Person B                   Conference with B regarding D
21 Invoice=2835951                                            <NA>

structure(list(timekeeper = c("Person A", "Invoice=2858502", 
NA, NA, "Person B", "Invoice=2835951", NA, NA, NA, NA, "Person C", 
"Invoice=2835951", NA, "Person D", "Invoice=2835951", NA, NA, 
NA, NA, "Person B", "Invoice=2835951"), narrative = c("Review and revise insert for audit response", 
"letter regarding separate investigation; review", "and exchange messages regarding same", 
NA, "Telephone conference with team; review e-mail", "correspondence from X regarding", 
"credentialing issues; e-mail correspondence", "with Y regarding same; review and", 
"approve transmittal letter for incident reports", NA, "Telephone conference with X, Y", 
"et al., regarding notice of", NA, "Telephone conference with", 
"Brady, Gibson, et al., regarding DAB status;", "telephone conference with X, et al.,", 
"regarding physician investigation at 123 and", "medical liability insurance; telephone", 
NA, "Conference with B regarding D", NA)), .Names = c("timekeeper", 
"narrative"), row.names = c(NA, -21L), class = "data.frame")

我想要的是这种格式:
timekeeper  combined narrative
Person A    Review and revise insert for audit response letter regarding separate investigation; review and exchange messages regarding same

一个可能的解决方案可能是在这个 SO question 中,但我的情况是空行和可变长度的叙述使我感到沮丧。 multiple rows combined

最佳答案

library(data.table)
library(zoo)   
#step 1: convert all timekeeper matching the invoice pattern to NA
#step 2: using `na.locf` from zoo package, fill in NA in timekeeper with most recent non-NA value
#step 3: collpase non-NA narrative by timekeeper

 setDT(df1)[,timekeeper:=na.locf(sub("(Invoice\\=\\d+)",NA,timekeeper))][,.(narrative=paste(narrative[!is.na(narrative)],collapse=" ")),by='timekeeper']

timekeeper
1:   Person A
2:   Person B
3:   Person C
4:   Person D
                                                                                                                                                                                                                                   narrative
1:                                                                                                          Review and revise insert for audit response letter regarding separate investigation; review and exchange messages regarding same
2: Telephone conference with team; review e-mail correspondence from X regarding credentialing issues; e-mail correspondence with Y regarding same; review and approve transmittal letter for incident reports Conference with B regarding D
3:                                                                                                                                                                                Telephone conference with X, Y et al., regarding notice of
4:                                           Telephone conference with Brady, Gibson, et al., regarding DAB status; telephone conference with X, et al., regarding physician investigation at 123 and medical liability insurance; telephone

关于r - 使用 R,将可变行数中的文本组合成单个文本元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31008209/

相关文章:

r - 将世界地图分为特定国家/地区的面板

r - sliderInput 的最小/最大值的交互式/响应式(Reactive)更改

r - 如何制作渐变彩虹调色板图?

java - Postgres DB 无法使用 RJDBC 连接到 R

r - 在 R 中访问多个艺术家的 Spotify API

r - 在 ggplot2 中组合图例

r - 如何纠正添加到所有输入特征中的大(未知)常数的线性回归?

r - 在 Revealjs 演示文稿中的 kable 表中使用 LaTeX 表达式

r - Shiny 的 mongolite 查询

r - 计算向量中每个值的对应十分位数