具有这样的数据框:
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))
还有一个名字列表
list <- c("Google", "Yahoo", "Amazon")
如何获得这样的输出:
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)
library(tidyverse)
library(reshape2)
library(zoo)
date_list_first= dframe[,head(.SD,1), by = .(id)]
答案 0 :(得分:2)
使用data.table
的方法如下:
library(data.table)
setDT(dframe)
date_list_first = dframe[order(date)][!duplicated(id,by=c('name','id'))]