r - How to remove rows based multiple conditions
问题描述
I want to delete the row based on conditions specified in two different columns, for each group. in my case, I want to remove "Death" which occurs in the first admission, but keep "Death" when it occurs in the readmission, for each patient's id
here is the initial data.frame :
ConditionI <- c("2017-01-01", "2018-01-01", "2018-01-15", "2018-01-20", "2018-02-01", "2018-02-1", "2018-03-01", "2018-04-01","2018-04-10")
ConditionII <- c("Death", "Alive", "Alive", "Death", "Alive", "Alive", "Death", "Alive", "Death")
id <- c("A","B","B","B","C","C","D","E","E")
df <- data.frame(id,ConditionI,ConditionII
my goal is :
ConditionII <- c( "Alive", "Alive", "Death", "Alive", "Alive", "Alive", "Death")
ConditionI <- c( "2018-01-01", "2018-01-15", "2018-01-20", "2018-02-01", "2018-02-1", "2018-04-01","2018-04-10")
id <- c("B","B","B","C","C","E","E")
df <- data.frame(id,ConditionI,ConditionII)
I thought this was a very basic question, but I tried several times and didn't get the answer. your help is very much appreciated. thanks in advance!
解决方案
我们可以直接使用subset
with duplicated
frombase R
subset(df, !id %in% id[!duplicated(id) & ConditionII == 'Death'])
# id ConditionI ConditionII
#2 B 2018-01-01 Alive
#3 B 2018-01-15 Alive
#4 B 2018-01-20 Death
#5 C 2018-02-01 Alive
#6 C 2018-02-1 Alive
#8 E 2018-04-01 Alive
#9 E 2018-04-10 Death
或与dplyr
library(dplyr)
df %>%
filter( !id %in% id[!duplicated(id) & ConditionII == 'Death'])
推荐阅读
- c++ - 引用在创建对象时更改值
- android - Firebase Auth 的清单合并失败
- html - 如何使用 Flexbox CSS 将每个元素放在一个角落?
- javascript - 等待 querySelectorAll
- bash - 如何从配置文件中匹配花括号 {} 中的字符串和打印行
- android - Android Studio 相对布局和线性布局不适合某些设备
- dropdown - HTA 中的多级下拉菜单未正确加载
- javascript - 如何使用 PHP 在全日历中显示事件
- javascript - 使用 Google Maps Geocoding API 确定位置的地址
- scala - 泛型函数中 T 的匿名推导