简体   繁体   中英

Conditionally removing rows of data in R

I have a large dataset and have tried finding an answer to this online but can't find one which answers my problem. I want to exclude a number of individuals in my dataset.

I have two columns of data in my dataset which are made up of a number of dates. I want to remove all individuals where date A is before dateB.

I've tried a variety of things but I can't work it out. When I have successfully removed these individuals all my other columns in the datset have NA in them versus the value they are meant to show.

If my dataset is called cohort, and dateA and dateB are the names of the columns, then this is what I tried to remove the individuals:

cohort2 <- cohort[!(cohort$dateA<cohort$dateB),]

cohort3 <- subset(cohort, cohort$dateA!<cohort$dateB)

Example of what my dataset looks like, I want to remove row 4 of the data in this case as date A occurs before date B. Thereby creating a new cohort which has the first three rows and the last row.

DateA DateB

1/1/16 1/1/15

2/2/16 2/2/15

3/3/16 3/3/15

4/4/15 4/4/16

NA___ 5/5/15

Using the dplyr package:


cohort <- data.frame(
  DateA = as.Date(c('1/1/2016', '2/2/2016', '3/3/2016', '4/4/2015', NA), format = "%m/%d/%Y"),
  DateB = as.Date(c('1/1/2015', '2/2/2015', '3/3/2015', '4/4/2016', '5/5/2015'), format = "%m/%d/%Y")
)

library(dplyr)


cohort %>% 
  filter(DateA > DateB | is.na(DateA) | is.na(DateB))

#>        DateA      DateB
#> 1 2016-01-01 2015-01-01
#> 2 2016-02-02 2015-02-02
#> 3 2016-03-03 2015-03-03
#> 4       <NA> 2015-05-05

Created on 2021-03-11 by the reprex package (v0.3.0)

Base R approach:

cohort <- data.frame(
  DateA = as.Date(c('1/1/2016', '2/2/2016', '3/3/2016', '4/4/2015', NA), format = "%m/%d/%Y"),
  DateB = as.Date(c('1/1/2015', '2/2/2015', '3/3/2015', '4/4/2016', '5/5/2015'), format = "%m/%d/%Y")
)

cohort[cohort$DateA > cohort$DateB | is.na(cohort$DateA) | is.na(cohort$DateB), ] 

#>        DateA      DateB
#> 1 2016-01-01 2015-01-01
#> 2 2016-02-02 2015-02-02
#> 3 2016-03-03 2015-03-03
#> 5       <NA> 2015-05-05

Created on 2021-03-11 by the reprex package (v0.3.0)

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