Create new DF based on text criteria of a certain column

Asked

Viewed 53 times

3

I have a DF in the following model,

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" contain the term "abc", there is no need to do any other function, only this same filter, not necessarily the "abc" will be at the beginning of the column.

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 R Base:

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 using the dplyr package:

library(dplyr)
df_nova <- filter(df, grepl("abc", x = coluna3))
  • Thank you Icaro! I was trying to use the grepl but I admit that I was getting in the parameters, a doubt, because it is used the "ignore.case = T"?

  • To make no difference between capital letters and minuscules. It is worth remembering that you can and should always use the "help" function of R itself and take a look at the documentation of the functions and their parameters in case of doubt. Often the answer will be there. In this example, just type: help("grepl") or ? grepl on the console. Remembering that this is for any function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.