我有一个带有日期的数据框,并且在单独的列中有一些文本
我想将日期在特定范围内的所有行粘贴在一起
输入
Date Text
12-01-2001 sometext
15-01-2001 sometext2
23-02-2015 Row3_Text
28-02-2015 Row4_Text
预期结果
Date Text
12-01-2001 sometextsometext2
23-02-2015 Row3_TextRow4_Text
尝试1
df<-df %>%
group_by(Date) %>%
summarise_all(.funs = function(x) paste(unique(c(dplyr::lag(x, default = NULL), x)), collapse = ":"))
问题
如何创建要分组的日期范围(以几天为单位,例如10天)。我敢肯定有一种润滑的方法可以做到这一点,但我无法弄清楚
答案 0 :(得分:2)
假设,您要按月分组并具有该月的第一个出现日期:
df1 %>%
group_by(month = lubridate::month(Date)) %>% # create group by month
summarise(Date = min(Date),
Text = paste0(Text, collapse = " ")) %>% # paste values together with a space
select(-month) # drop the month group
# A tibble: 2 x 2
Date Text
<chr> <chr>
1 12-01-2001 sometext sometext2
2 23-02-2015 Row3_Text Row4_Text
答案 1 :(得分:2)
这是问题的第二版本的一种解决方案,可以按日期范围进行汇总。
df %>%
mutate(Range = cumsum(c(0L, diff(Date) > 10))) %>%
group_by(Range) %>%
summarise(Date = first(Date),
Text = paste(Text, collapse = ":")) %>%
select(-Range)
## A tibble: 2 x 2
# Date Text
# <date> <chr>
#1 2001-01-12 sometext:sometext2
#2 2015-02-23 Row3_Text:Row4_Text
数据。
df <- read.table(text = "
Date Text
12-01-2001 sometext
15-01-2001 sometext2
23-02-2015 Row3_Text
28-02-2015 Row4_Text
", header = TRUE, stringsAsFactors = FALSE)
df$Date <- as.Date(df$Date, "%d-%m-%Y")