简体   繁体   English

根据 r 中的条件分组、删除重复和交换值

[英]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我想根据 ID 和 Serial 对表进行分组,然后只保留那些在 DATE 中出现最新的 VALUE,同时将日期交换为较早的值我还希望保留与 ID 和 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我可以处理的代码是使用 dplyr 进行分组我不确定如何继续使用 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"
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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