Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removing rows that do not match a certain condition in r

I have a dataframe with multiple columns and I would like to remove rows that do not meet a certain condition. I would like to only keep the rows that have a -1 followed by a 1 in the dataframe in one of the columns.

Example data.frame

    column a   column b
1     1          1
2     3         -1
3     8          1
4     10        -1
5     12         1
6     15         1

Example output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    column a   column b
1     3         -1
2     8          1
3     10        -1
4     12         1

example dataframe
example output

>Solution :

With dplyr you can use:

library(dplyr)
your_data %>%
  filter(
    (`column b` == -1 & lead(`column b`) == 1) |
    (`column b` == 1 & lag(`column b`) == -1)
  )
#   column a column b
# 2        3       -1
# 3        8        1
# 4       10       -1
# 5       12        1

Using this input data:

your_data = read.table(text = '    "column a"   "column b"
1     1          1
2     3         -1
3     8          1
4     10        -1
5     12         1
6     15         1', header = T, check.names = FALSE)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading