sar
sar

Reputation: 192

select text from multiple combinations of text within a dataframe R

I want to subset data based on a text code that is used in numerous combinations throughout one column of a df. I checked first all the variations by creating a table.

 list <-  as.data.frame(table(EQP$col1))

enter image description here

I want to search within the dataframe for the text "EFC" (even when combined with other letters) and subset these rows so that I have a resultant dataframe that looks like this.

enter image description here

I have looked through this question here, but this does not answer the question. I have reviewed the tidytext package, but this does not seem to be the solution either.

How to Extract keywords from a Data Frame in R

Upvotes: 0

Views: 932

Answers (2)

maarvd
maarvd

Reputation: 1284

Another option besides the mentioned solution by Gallarus would be:

library(stringr)
library(dplyr)
df %>% filter(str_detect(Var1, "EFC"))

As described by Sam Firke in this post:

Selecting rows where a column has a string like 'hsa..' (partial string match)

Upvotes: 1

Gallarus
Gallarus

Reputation: 476

You can simply use grepl.

Considering your data.frame is called df and the column to subset on is col1

df <- data.frame(
    col1 = c("eraEFC", "dfs", "asdj, aslkj", "dlja,EFC,:LJ)"),
    stringsAsFactors = F
)

df[grepl("EFC", df$col1), , drop = F]

Upvotes: 1

Related Questions