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

How do I remove elements by indices from a list?

I have two data frames. One, we’ll call df, is very large with multiple columns and rows. One such column is "admin" and another is "name."

I created a second data frame called xmatches by searching for indices of rows that contain a certain admin and a certain name. I would like to remove these rows from df. How would I achieve this?

I tried just so I could take a look at what I might be removing and it threw an error (I am very new at this, but did expect some error which is why I didn’t attempt an actual removal here.)

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

a_df[xmatches]

Error:

Can't subset columns past the end.
ℹ Locations 52762, 52763, 52764, …, 52981, and 52982 don't exist.
ℹ There are only 16 columns.
Run `rlang::last_error()` to see where the error occurred.

>Solution :

Why make a separate matches df? Just do:

df <- df %>%
  filter(!str_detect(colA, some_regex) & !str_detect(colB, some_regex))

Alternatively, if there is only one admin and name to remove:

df <- df %>%
  filter(name_col != name & admin_col != admin)

Let me know if this works.

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