Created
December 5, 2018 05:00
-
-
Save korkridake/5bf1eb74ca25230047d063e3d8b3be2e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| data <- structure(list(CODE = c("AA","AA","AA","AA","AA"), | |
| DATE = c(20181205,20181205,20181205,20181212,20181212), | |
| TIME = c(14.0,14.0,14.2,18.0,18.0), | |
| NO = c("D1","D1","D2","D3","D3"), | |
| ITEM = c("AK011","ZH002","HJ005","AK011","KK021"), | |
| RANK = c(1,1,2,3,3)), | |
| .Names = c("CODE", "DATE", "TIME", "NO", "ITEM","RANK"), | |
| class = c("tbl_df", "tbl", "data.frame"), | |
| row.names = c(NA, -5L)) | |
| library(dplyr) | |
| data_unique = data[!duplicated(data[c("CODE","DATE", "TIME", "NO")]),] | |
| # # A tibble: 3 x 6 | |
| # CODE DATE TIME NO ITEM RANK | |
| # <chr> <dbl> <dbl> <chr> <chr> <dbl> | |
| # 1 AA 20181205 14 D1 AK011 1 | |
| # 2 AA 20181205 14.2 D2 HJ005 2 | |
| # 3 AA 20181212 18 D3 AK011 3 | |
| data_unique = data_unique %>% mutate(RANK_dplyr = 1:n()) | |
| # A tibble: 3 x 7 | |
| # CODE DATE TIME NO ITEM RANK RANK_dplyr | |
| # <chr> <dbl> <dbl> <chr> <chr> <dbl> <int> | |
| # 1 AA 20181205 14 D1 AK011 1 1 | |
| # 2 AA 20181205 14.2 D2 HJ005 2 2 | |
| # 3 AA 20181212 18 D3 AK011 3 3 | |
| data %>% | |
| inner_join(data_unique, by = c("CODE","DATE", "TIME", "NO")) | |
| # A tibble: 5 x 9 | |
| # CODE DATE TIME NO ITEM.x RANK.x ITEM.y RANK.y RANK_dplyr | |
| # <chr> <dbl> <dbl> <chr> <chr> <dbl> <chr> <dbl> <int> | |
| # 1 AA 20181205 14 D1 AK011 1 AK011 1 1 | |
| # 2 AA 20181205 14 D1 ZH002 1 AK011 1 1 | |
| # 3 AA 20181205 14.2 D2 HJ005 2 HJ005 2 2 | |
| # 4 AA 20181212 18 D3 AK011 3 AK011 3 3 | |
| # 5 AA 20181212 18 D3 KK021 3 AK011 3 3 | |
| # ----------------------------------- | |
| # or even a simpler solution | |
| # one-liner | |
| # ----------------------------------- | |
| data %>% group_indices(CODE, DATE, TIME, NO) | |
| # [1] 1 1 2 3 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment