取最大值并移至新列

Take maximum values and move into new columns

ID Date      Paid
1  1/1/2006  0
1  2/5/2010  0
2  5/3/2013  0
2  6/7/2018  0

我想按 ID 获取最大日期并将这些行中的信息分散到新列中:

ID Date      Paid   Max. Date Max. Paid
1  1/1/2006  0   2/5/2010  0
2  5/3/2013  0   6/7/2018  0

我该怎么做?

我们可以做到

library(dplyr)
df1 %>%  
    mutate(Date = as.Date(Date, '%m/%d/%Y')) %>%
    arrange(ID, Date) %>%
    group_by(ID) %>% 
    summarise(Max.Date = last(Date), Max.Paid = last(Paid), 
          Paid = first(Paid), Date = first(Date))    
# A tibble: 2 x 5
#     ID Max.Date   Max.Paid Paid  Date      
#  <int> <date>     <chr>    <chr> <date>    
#1     1 2010-02-05 0     0  2006-01-01
#2     2 2018-06-07 0     0  2013-05-03

数据

df1 <- structure(list(ID = c(1L, 1L, 2L, 2L), Date = c("1/1/2006", "2/5/2010", 
"5/3/2013", "6/7/2018"), Paid = c("0", "0", "0", "0"
)), class = "data.frame", row.names = c(NA, -4L))