Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete rows from a data.frame, based on an external list, using R?

Tags:

dataframe

r

This might be an easy question but i still need some help for using R.

I have a data.frame (main_data), lets say..

NAMES   AGE     LOC Jyo     23      Hyd Abid    27      Kar Ras     24      Pun Poo     25      Goa Sus     28      Kar 

I wish to remove a few rows based on a list of names. So lets say I have another list of table as follows:

NAMES_list Jyo Ras Poo 

So based on this list, if any of the names match to my above "main_data" table, then I would like to remove the whole row contianing them, so the result should be as follows

NAMES   AGE     LOC Abid    27      Kar Sus     28      Kar 

Can anyone help me how to achive this using R? Thanks in advance.. :)

like image 825
Letin Avatar asked Oct 22 '12 13:10

Letin


People also ask

How do you delete a row from a Dataframe in R based on a condition?

For example, we can use the subset() function if we want to drop a row based on a condition. If we prefer to work with the Tidyverse package, we can use the filter() function to remove (or select) rows based on values in a column (conditionally, that is, and the same as using subset).

How do I delete specific rows in R?

To remove the rows in R, use the subsetting in R. There is no built-in function of removing a row from the data frame, but you can access a data frame without some rows specified by the negative index. This process is also called subsetting. This way, you can remove unwanted rows from the data frame.

How do I remove rows from two conditions in R?

To remove rows of data from a dataframe based on multiple conditional statements. We use square brackets [ ] with the dataframe and put multiple conditional statements along with AND or OR operator inside it. This slices the dataframe and removes all the rows that do not satisfy the given conditions.


1 Answers

Use %in%:

main_data2 <- main_data[ ! main_data$NAMES %in% NAMES_list, ] 
like image 191
January Avatar answered Oct 01 '22 13:10

January