简体   繁体   中英

Group by, remove duplicates and swap value based on condition in r

I've a table as under

+----+--------+-------+------------+
| ID | Serial | VALUE |    DATE    |
+----+--------+-------+------------+
|  1 |     11 |    -1 | 2019-10-01 |
|  1 |     11 |    -2 | 2019-10-02 |
|  2 |     22 |    -9 | 2019-09-01 |
|  2 |     22 |   -10 | 2019-09-02 |
|  2 |     12 |     9 | 2019-09-03 |
|  3 |     12 |   -10 | 2019-08-01 |
|  3 |     12 |    -8 | 2019-08-03 |
|  3 |     13 |    -7 | 2019-08-04 |
+----+--------+-------+------------+

I want to group the table based on ID and Serial and then keep only those VALUE in group which appear the latest in DATE while swapping the date to earlier value I also wish to keep rows which dont have any duplicates with respect to the ID and Serial

My desired result is as under

+----+--------+-------+------------+
| ID | Serial | VALUE |    DATE    |
+----+--------+-------+------------+
|  1 |     11 |    -2 | 2019-10-01 |
|  2 |     22 |   -10 | 2019-09-01 |
|  2 |     12 |     9 | 2019-09-03 |
|  3 |     12 |    -8 | 2019-08-01 |
|  3 |     13 |    -7 | 2019-08-04 |
+----+--------+-------+------------+ 

the code I could work on was to group by using dplyr I'm not sure how do I proceed for the rest

My code until now is as under

df %>%
group by (ID, SERIAL)

Here is one idea.

library(tidyverse)

dat %>%
  mutate(DATE = as.Date(DATE)) %>%
  group_by(ID, Serial) %>%
  summarize(VALUE = last(VALUE), DATE = min(DATE)) %>%
  ungroup() %>%
  arrange(ID, DATE)
# # A tibble: 5 x 4
#      ID Serial VALUE DATE      
#   <dbl>  <dbl> <dbl> <date>    
# 1     1     11    -2 2019-10-01
# 2     2     22   -10 2019-09-01
# 3     2     12     9 2019-09-03
# 4     3     12    -8 2019-08-01
# 5     3     13    -7 2019-08-04

DATA

# Create an example
dat <- tribble(
  ~ID, ~Serial, ~VALUE, ~DATE,
    1,      11,      -1, "2019-10-01",
    1,      11,      -2, "2019-10-02",
    2,      22,      -9, "2019-09-01",
    2,      22,     -10, "2019-09-02",
    2,      12,       9, "2019-09-03",
    3,      12,     -10, "2019-08-01",
    3,      12,      -8, "2019-08-03",
    3,      13,      -7, "2019-08-04"
)

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