【发布时间】:2020-01-27 20:58:32
【问题描述】:
我想保留外部列表:
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
怎么可能?
使用this,它只保留每个id的第一条记录,而不是列表中第一次记录的每个值
library(data.table)
setDT(dframe)
date_list_first = dframe[order(date)][!duplicated(id)]
【问题讨论】: