2017-06-14 62 views
-1

我正在尋找一種方法將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) 

如果有人能幫助我,沒有準備的數據,以便圍繞解決這個問題的一種方式,但一個「容易」的方式拋出了一句,我會很優雅。

回答

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

一個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)