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

Is there a R function to delete an element in data frame

I’m looking for a way to delete an element out of a data frame (delete the full row) if a certain word is not found in the Name column. In my case, the element is a color like red,blue,green

The dataset looks like this:

Name|Classification
A red apple | Fruit
A banana | Fruit
A blue carrot | Vegetable

I use the following code to loop through the dataframe and check if a color is in the string of Name column. Currently only looking at red to make it work.

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

for(i in 1:length(nsl)){
if(!grepl("red",nsl[[1:i]],fixed=TRUE)){
nsl[[1:i]] <- " " #This is where I want to delete the item or set it to empty (so later on I can filter all empty out)
}
}

But I have no clue how it is done on a data frame that has two columns/dimensions. All internet tutorials I find tell me to use nsl[-index_element_here] but that deletes the column Classification. Which they use on examples of 1-D lists. Or tell me to use %in% which is , I think, not what I’m looking for.

The expected end result is a dataframe where only fruit and vegetables with color are in. Non-colour is deleted.

>Solution :

There is no need for a loop:

nsl <- read.table(text = "Name|Classification
'A red apple' | Fruit
'A banana' | Fruit
'A blue carrot' | Vegetable
", header = TRUE, sep = "|")

nsl[!grepl("red", nsl$Name, fixed = TRUE), ]
#>             Name Classification
#> 2      A banana           Fruit
#> 3 A blue carrot       Vegetable
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