此玩具数据框表示人员输入的时间。我可以使用的格式有完全相同的模式,用于同一个人和同一天的多个文本条目。同一个人和同一天最多可以输入15个文本。多文本条目的行中没有人员条目。
structure(list(Date = structure(c(1514764800, 1514764800, NA,
1517443200, 1519862400, NA, NA, NA, 1519862400, NA, NA), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), Person = c("FMC", "ABC", NA, "FMC",
"ABC", NA, NA, NA, "RWM", NA, NA), Text = c("work on request",
"More text", "third line", "email to re: summary", "work on loan documents",
"sixth line of text", "text seven", "eighth in a series", "conferences with working group",
"line ten", "review and provide comments")), row.names = c(NA,
-11L), class = c("tbl_df", "tbl", "data.frame"))
如何组合文本元素,以便每个人的条目每天只有一行 ,删除不需要的行(一旦文本粘贴在一起)并到达以下对象?
编辑后的问题忽略了我尝试失败的for
循环。
必须有一种方法可以将给定日期的所有文本合并为给定日期的一行(例如,ABC在2018年1月1日有两个条目)并删除合并后的行文字来了。
答案 0 :(得分:3)
我们可以使用na.locf
用最后一个非缺失值填充缺失值(NA
),然后group_by
连续出现Person
和summarise
通过Text
一起paste
来实现。
library(dplyr)
library(zoo)
library(data.table)
df %>%
na.locf(.) %>%
group_by(group = rleid(Person)) %>%
summarise(Text = paste0(Text, collapse = " "))
# group Text
# <int> <chr>
#1 1 work on request
#2 2 More text third line
#3 3 email to re: summary
#4 4 work on loan documents sixth line of text text seven eighth in a series
#5 5 conferences with working group line ten review and provide comments
对于更新后的问题,我们可以
library(dplyr)
library(zoo)
df %>%
na.locf(.) %>%
group_by(Date, Person) %>%
summarise(Text = paste0(Text, collapse = " "))
答案 1 :(得分:1)
无需复杂,只需使用tidyverse
。
针对问题的更改进行了调整:
library(tidyverse)
> df%>%
fill(Date:Person, Date:Person) %>% # Fills missing values in using the previous entry.
group_by(Date, Person) %>%
summarise(Text = paste(Text, collapse = ' '))
# A tibble: 5 x 3
Date Person Text
<dttm> <chr> <chr>
1 2018-01-01 00:00:00 ABC More text third line
2 2018-01-01 00:00:00 FMC work on request
3 2018-02-01 00:00:00 FMC email to re: summary
4 2018-03-01 00:00:00 ABC work on loan documents sixth line of text text seven eighth in a series
5 2018-03-01 00:00:00 RWM conferences with working group line ten review and provide comments
数据:
# A tibble: 11 x 3
Date Person Text
<dttm> <chr> <chr>
1 2018-01-01 00:00:00 FMC work on request
2 2018-01-01 00:00:00 ABC More text
3 NA NA third line
4 2018-02-01 00:00:00 FMC email to re: summary
5 2018-03-01 00:00:00 ABC work on loan documents
6 NA NA sixth line of text
7 NA NA text seven
8 NA NA eighth in a series
9 2018-03-01 00:00:00 RWM conferences with working group
10 NA NA line ten
11 NA NA review and provide comments
答案 2 :(得分:0)
library(dplyr)
merge_lines <- function(x) paste(x, collapse = ' ')
df %>%
zoo::na.locf(.) %>%
group_by(Person) %>%
summarise_at(vars(Text), (funs(merge_lines)))
结果:
# A tibble: 4 x 2
Person Text
<chr> <chr>
1 ABC More text third line
2 FMC work on request email to re: summary
3 HIL work on loan documents sixth line of text text seven eighth in a series
4 RWM conferences with working group line ten review and provide comments