library(data.table)
setDT(df)
df[, lapply(.SD, max), ID]
# ID S1 S2 S3 S4 Date
# 1: ex1 1 1 0 0 2016-06-08
# 2: ex2 0 0 1 0 2015-05-05
# 3: ex3 1 1 0 1 2015-06-07
# 4: ex4 0 1 0 0 2009-08-07
# 5: ex5 1 1 1 0 2017-06-12
Это также работает:
library(dplyr)
df %>%
group_by(ID) %>%
summarise_all(max)
или в базе R:
do.call(rbind
, lapply(split(df, df$ID)
, function(g) data.frame(lapply(g, max))))
Используемые данные:
df <- fread("
a ID S1 S2 S3 S4 Date
1 ex1 1 0 0 0 4/7/12
2 ex1 0 1 0 0 6/8/16
3 ex2 0 0 1 0 5/5/15
4 ex3 1 1 0 0 4/19/13
5 ex3 0 1 0 1 6/7/15
6 ex4 0 1 0 0 8/7/09
7 ex5 1 1 1 0 6/12/17
")[, -1]
df[, Date := lubridate::mdy(Date)]