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 to subset a data frame based on specific or similar strings

As an example, I created a data like this

df<- structure(list(Names = c("AA", "ab", "CC", "AVY"), Column1 = c(0L, 
0L, 0L, 1L), Column2 = c(0L, 0L, 0L, 0L), Column3 = c(0L, 0L, 
0L, 1L)), class = "data.frame", row.names = c(NA, -4L))

I want to subset and keep those rows that have my strings in them lets say 3 of them
if I want to find the position, I can using the following to find where CC is

which(df=="CC", arr.ind=TRUE)

or if I want to find two of them CC and ab , I can

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

which(df==c("CC","ab"), arr.ind=TRUE)

but what I want is to subset it in a new data frame like below

Name    Column1 Column2 Column3
ab       0       0       0
CC       0       0       0

I found for one string at the time based on other people solutions but still if I have more than one I dont know how to do it

df[sapply(df, function(x) grepl("CC",x)),]

>Solution :

Use %in% or subset, no need for which or sapply.

df[df$Names %in% c("CC","ab"), ]
#   Names Column1 Column2 Column3
# 2    ab       0       0       0
# 3    CC       0       0       0

subset(df, Names %in% c("CC","ab"))
#   Names Column1 Column2 Column3
# 2    ab       0       0       0
# 3    CC       0       0       0

%in% is a close relative of match.

df$Names %in% c("CC","ab")
# [1] FALSE  TRUE  TRUE FALSE

basically does

!is.na(match(df$Names, c("CC","ab")))
# [1] FALSE  TRUE  TRUE 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