按日期分组并将行折叠为单个行

时间:2017-06-14 14:23:53

标签: r dataframe

我正在寻找一种方法来将df转向dfres。

Dfres是

obj <- date      #where type == I5,
min <- min(date) #where type == I6, 
max <- max(date) #where type == I6,

所有这些按年份分组。

year <- c('2014','2015','2016','2017','2014','2015','2016','2017','2016','2014','2015')
type <- c('I6','I6','I6','I6','I6','I6','I6','I6','I5','I5','I5')
date <- c('2014-06-03','2015-08-01','2016-06-01','2017-05-15',
'2014-04-11','2015-03-14','2016-03-17','2017-03-08','2016-11-05',
'2014-09-04','2015-05-01')
df <- data.frame(year,type,date)

year  <- c('2014','2015','2016','2017')
obj   <- c('2014-09-04','2015-05-01','2016-11-05',NA)
min   <- c('2014-04-11','2015-03-14','2016-03-17','2017-03-08')
max   <- c('2014-06-03', '2015-08-01','2016-06-01','2017-05-15')
dfres <- data.frame(year,obj,min,max)

如果有人可以帮助我,不是为了解决这个问题而准备数据,而是以一种“轻松”的方式抛出一个句子,我会优雅的。

2 个答案:

答案 0 :(得分:1)

使用dplyr的想法是,

library(dplyr)

df %>% 
 filter(type == 'I6') %>% 
 group_by(year) %>% 
 summarise(min_d = min(date), max_d = max(date)) %>% 
 full_join(df[df$type == 'I5',], ., by = 'year') %>% 
 select(-type) %>% 
 arrange(year)

#  year       date      min_d      max_d
#1 2014 2014-09-04 2014-04-11 2014-06-03
#2 2015 2015-05-01 2015-03-14 2015-08-01
#3 2016 2016-11-05 2016-03-17 2016-06-01
#4 2017       <NA> 2017-03-08 2017-05-15

答案 1 :(得分:1)

data.table方法是:

 library(data.table)
 setDT(df)
 i5 <- df[type == 'I5', .(obj = date), by = year]
 i6 <- df[type == 'I6', .(min = min(as.Date(date)), max = max(as.Date(date))), by = year]
 dfres <- merge(i5, i6, by = 'year', all = TRUE)