2014-12-22 116 views
1

比方說我有兩個變量:ID和日期:舊日期刪除重複

ID <- c(1,1,1,2,2,2,3,3,3,4,4,4) 
Datestr <- c("01/05/2014", "01/16/2014", "01/08/2014","07/05/2014", "07/01/2014", "07/02/2014", "02/05/2014", "02/11/2014", "02/02/2014","01/01/2014", "01/11/2014", "01/03/2014") 
dates <- as.Date(Datestr, "%m/%d/%Y") 
Mydata <- data.frame(ID, dates) 


    ID dates 
1 1 2014-01-05 
2 1 2014-01-16 
3 1 2014-01-08 
4 2 2014-07-05 
5 2 2014-07-01 
6 2 2014-07-02 
7 3 2014-02-05 
8 3 2014-02-11 
9 3 2014-02-02 
10 4 2014-01-01 
11 4 2014-01-11 
12 4 2014-01-03 

現在,我需要刪除重複並保持ID與最近的日期。

ID dates 
1 1 2014-01-05 
5 2 2014-07-01 
9 3 2014-02-02 
10 4 2014-01-01 

回答

4

您可以使用aggregate

aggregate(dates ~ ID, Mydata, min) 
    ID  dates 
1 1 2014-01-05 
2 2 2014-07-01 
3 3 2014-02-02 
4 4 2014-01-01 

或者

library(dplyr) 
group_by(Mydata, ID) %>% filter(min_rank(dates) == 1L) 
#Source: local data frame [4 x 2] 
#Groups: ID 
# 
# ID  dates 
#1 1 2014-01-05 
#2 2 2014-07-01 
#3 3 2014-02-02 
#4 4 2014-01-01 

或者

group_by(Mydata, ID) %>% slice(which.min(dates)) 
#Source: local data frame [4 x 2] 
#Groups: ID 
# 
# ID  dates 
#1 1 2014-01-05 
#2 2 2014-07-01 
#3 3 2014-02-02 
#4 4 2014-01-01 

或者

group_by(Mydata, ID) %>% arrange(dates) %>% slice(1) 
#Source: local data frame [4 x 2] 
#Groups: ID 
# 
# ID  dates 
#1 1 2014-01-05 
#2 2 2014-07-01 
#3 3 2014-02-02 
#4 4 2014-01-01 

並有data.table選項:

library(data.table) 
setDT(Mydata)[,.SD[which.min(dates)], ID] 
# ID  dates 
#1: 1 2014-01-05 
#2: 2 2014-07-01 
#3: 3 2014-02-02 
#4: 4 2014-01-01 
4

如果你想只爲每個ID的第一次約會,你可以使用min

使用dplyr

library(dplyr) 
group_by(Mydata, ID) %>% summarise(dates = min(dates)) 
# ID  dates 
# 1 1 2014-01-05 
# 2 2 2014-07-01 
# 3 3 2014-02-02 
# 4 4 2014-01-01 

或者data.table

library(data.table) 
as.data.table(Mydata)[, .(dates = min(dates)), by = ID][] 
# ID  dates 
# 1: 1 2014-01-05 
# 2: 2 2014-07-01 
# 3: 3 2014-02-02 
# 4: 4 2014-01-01 
3

使用duplicated將是最有效的方法IMO

Mydata <- Mydata[order(Mydata$dates), ] 
Mydata[!duplicated(Mydata$ID), ] 
# ID  dates 
# 10 4 2014-01-01 
# 1 1 2014-01-05 
# 9 3 2014-02-02 
# 5 2 2014-07-01 

或者使用data.table S表示額外的效率增益

library(data.table) 
unique(setorder(setDT(Mydata), dates), by = "ID") 
# ID  dates 
# 1: 4 2014-01-01 
# 2: 1 2014-01-05 
# 3: 3 2014-02-02 
# 4: 2 2014-07-01 

或者duplicated

setorder(setDT(Mydata), dates)[!duplicated(ID)] 
# ID  dates 
# 1: 4 2014-01-01 
# 2: 1 2014-01-05 
# 3: 3 2014-02-02 
# 4: 2 2014-07-01