我想避免使用外部列表:
list <- c("Google", "Yahoo", "Amazon")
数据框中记录在第一个时间戳(最旧的时间戳)中的值,如下所示:
dframe <- structure(list(id = c(1L, 1L, 1L, 1L, 2L, 2L, 2L), name = c("Google",
"Google", "Yahoo", "Amazon", "Amazon", "Google", "Amazon"), date = c("2008-11-01",
"2008-11-02", "2008-11-01", "2008-11-04", "2008-11-01", "2008-11-02",
"2008-11-03")), class = "data.frame", row.names = c(NA, -7L))
预期的输出是这样的:
id name date 1 Google 2008-11-01 1 Yahoo 2008-11-01 1 Amazon 2008-11-04 2 Amazon 2008-11-01 2 Google 2008-11-02
如何做到这一点?
使用此功能,它仅保留每个id的第一条记录,而不保留第一次记录的列表中的每个单个值的第一条记录
library(data.table)
setDT(dframe)
date_list_first = dframe[order(date)][!duplicated(id)]
一个选项使用 base R
dframe$date <- as.Date(dframe$date)
aggregate(date~ ., dframe, min)
# id name date
#1 1 Amazon 2008-11-04
#2 2 Amazon 2008-11-01
#3 1 Google 2008-11-01
#4 2 Google 2008-11-02
#5 1 Yahoo 2008-11-01
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句