Create new DF based on certain column text criteria

3

I have a DF in the following template,

Data    Coluna 1    Coluna 2    Coluna 3
12/01/2016  82       88           abc
06/02/2016  30       76           abd
15/03/2016  9        74           abc_abc|1234
11/01/2016  43       48           abc_abc|1235
14/04/2016  21       100          abd_abd|1234
14/04/2016  28       21           abd_abd|1235
15/01/2016  50       16           abc_abc|1236
19/01/2016  14       66           abd_abd|1231
26/02/2016  14       73           abc_abc|1239

I want to leave the lines only where in "Column 3" they contain the term "abc", it is not necessary to do any other function, only this same filter, not necessarily "abc" will be at the beginning of the column. >     

asked by anonymous 09.05.2016 / 21:34

1 answer

3

Just use the grepl command

# Dados de exemplo
df <- data.frame(coluna1 = 1:5, coluna3 = c("abc", "abd", "abc_abc|1234", "abd_abd|1235", "avc-abc|1239"))

With Base R:

df_nova <- df[grepl(pattern = "abc", x = df$coluna3, ignore.case = TRUE ),]

df_nova
>coluna1      coluna3
1       1          abc
3       3 abc_abc|1234
5       5 avc-abc|1239

Or by using the dplyr package:

library(dplyr)
df_nova <- filter(df, grepl("abc", x = coluna3))
    
09.05.2016 / 21:50