根据多个条件从数据框中删除整个ID [英] Remove entire IDs from the dataframe based on multiple conditions

查看:42
本文介绍了根据多个条件从数据框中删除整个ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从患者数据集中删除具有多个条件的整个ID,其中应完全删除等于fvdate的记录日期。我的数据集看起来像是

I want to remove the entire ids from the patient data set with multiple condition in which the record date equal to fvdate should be removed completely. my data set will look like

ID  Record date Disease  Fvdate
1   02-03-2012   0     02-03-2012
2   03-03-2013   1     03-03-2013
3   04-03-2014   0     04-03-2014
3   03-03-2015   1     03-03-2015
4   06-06-2016   1     02-03-2012
4   07-06-2017   1     03-03-2013
5   08-05-2018   1     04-03-2014
6   09-06-2019   0     03-03-2015

我希望输出为

ID  Record date Disease  Fvdate
1   02-03-2012   0    02-03-2012
2   03-03-2013   1    03-03-2013
4   06-06-2016   1    02-03-2012
4   07-06-2017   1    03-03-2013
5   08-05-2018   1    04-03-2014
6   09-06-2019   0    03-03-2015

示例输入数据:

d <- read.table(text = "
ID  Record_date Disease  Fvdate
1   02-03-2012   0     02-03-2012
2   03-03-2013   1     03-03-2013
3   04-03-2014   0     04-03-2014
3   03-03-2015   1     03-03-2015
4   06-06-2016   1     02-03-2012
4   07-06-2017   1     03-03-2013
5   08-05-2018   1     04-03-2014
6   09-06-2019   0     03-03-2015", header = TRUE, stringsAsFactors = FALSE)


推荐答案

条件是日期不相同的过滤器 OR 每个ID的行数是1:

Filter on condition that dates are not the same OR number of rows per ID is 1:

library(dplyr)

d %>% 
  group_by(ID) %>% 
  filter(!Record_date == Fvdate | n() == 1)

#   ID Record_date Disease     Fvdate
# 1  1  02-03-2012       0 02-03-2012
# 2  2  03-03-2013       1 03-03-2013
# 3  4  06-06-2016       1 02-03-2012
# 4  4  07-06-2017       1 03-03-2013
# 5  5  08-05-2018       1 04-03-2014
# 6  6  09-06-2019       0 03-03-2015

这篇关于根据多个条件从数据框中删除整个ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆