简体   繁体   中英

Find values from one column in another column according to ID in r

I have a data frame with multiple entries for each ID. An ID has a reference number (NEW_REF) and an old reference number (OLD_REF). I need to find the most recent reference number for each ID, meaning the reference number that is not in the Old reference number column.

ID <- c(1,2,3,4,1,3,5,2,4,1,3,4)     
NEW_REF <- c("TS101","TS253","TS565","TS789","TD123","TS101","TD367","TH152","TD123","TF908","TD256","TS898")
OLD_REF <- c("TD123","TH152","TS101","TD123","TF908","TD256","TG232","TR142","TS898","TR268","TB496","TD969")
DF <- data.frame(ID,NEW_REF ,OLD_REF )

DF$Active_ind <- NA
DF$Active_ind[which(DF$NEW_REF %in% DF$OLD_REF )] <-"N"    #if a reference number is in the old reference number column it is not active or not the most recent
DF$Active_ind[which(!(DF$NEW_REF %in% DF$OLD_REF ))] <-"Y"   #if a reference number is not in the old reference number column it is active or the most recent

    ID NEW_REF OLD_REF Active_ind
1   1   TS101   TD123          N
2   2   TS253   TH152          Y
3   3   TS565   TS101          Y
4   4   TS789   TD123          Y
5   1   TD123   TF908          N
6   3   TS101   TD256          N
7   5   TD367   TG232          Y
8   2   TH152   TR142          N
9   4   TD123   TS898          N
10  1   TF908   TR268          N
11  3   TD256   TB496          N
12  4   TS898   TD969          N

My problem is that ID 1 has a new reference TS101 (row 1) and ID 3 has an old reference TS101 (row 3). How do I check which reference number is most recent per ID if the reference numbers are not unique.

I would like Row 1 to have a Y in the Active_ind column:

    ID NEW_REF OLD_REF Active_ind
1   1   TS101   TD123          Y
2   2   TS253   TH152          Y
3   3   TS565   TS101          Y
4   4   TS789   TD123          Y
5   1   TD123   TF908          N
6   3   TS101   TD256          N
7   5   TD367   TG232          Y
8   2   TH152   TR142          N
9   4   TD123   TS898          N
10  1   TF908   TR268          N
11  3   TD256   TB496          N
12  4   TS898   TD969          N

I know it is possible with a for loop, but I would like to avoid it as my data set has over 40 000 different IDs and becomes very time intensive when a loop is used.

We can use dplyr to group them by ID and then check if the values in NEW_REF is present in OLD_REF and give them the values accordingly.

library(dplyr)
DF %>%
   group_by(ID) %>%
   mutate(Active_Ind = ifelse(NEW_REF %in% OLD_REF, "N", "Y"))


#     ID NEW_REF OLD_REF Active_Ind
#   <dbl>  <fctr>  <fctr>      <chr>
#      1   TS101   TD123          Y
#      2   TS253   TH152          Y
#      3   TS565   TS101          Y
#      4   TS789   TD123          Y
#      1   TD123   TF908          N
#      3   TS101   TD256          N
#      5   TD367   TG232          Y
#      2   TH152   TR142          N
#      4   TD123   TS898          N
#      1   TF908   TR268          N
#      3   TD256   TB496          N
#      4   TS898   TD969          N

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM