这是一个选项dplyr
。按“ID”分组,使用hm
from将“时间”转换为时间类lubridate
,获取最大值(which.max
)的索引并根据该索引对“值”进行子集化
library(dplyr)
library(lubridate)
df1 %>%
group_by(ID) %>%
mutate(Final_value = Value[which.max(as.numeric(hm(Time)))])
# A tibble: 5 x 4
# Groups: ID [2]
# ID Time Value Final_value
# <int> <chr> <int> <int>
#1 1 1:20 1 2
#2 1 2:43 2 2
#3 1 1:56 3 2
#4 2 1:10 4 4
#5 2 1:05 5 4
或使用base R
df1$Final_value <- with(df1, Value[as.logical(ave(as.numeric(as.POSIXct(Time,
format = '%H:%M')),
ID, FUN = function(x) x== max(x)))][ID])
或者另一种选择是order
基于“时间”和“ID”的数据,然后last
用ave
i1 <- with(df1, order(ID, as.POSIXct(TIme, format = '%H:%M')))
with(df1[i1,], ave(Value, ID, FUN = function(x) x[length(x)]))
数据
df1 <- structure(list(ID = c(1L, 1L, 1L, 2L, 2L), Time = c("1:20", "2:43",
"1:56", "1:10", "1:05"), Value = 1:5), class = "data.frame", row.names = c(NA,
-5L))