-6

・This is the table , name: x

==================================================================
 # A tibble: 6 x 10
     ID   blood  age  hair_color name height weight distance expiry
    <dbl> <chr> <dbl>    <chr>  <chr>  <dbl>  <dbl>    <dbl>  <dbl>
1     1     P    60     yellow  john    999    128      110    120
2     2     N    52      black   tom    999    180      156    160
3     3     P   100      white   tom    999    190      140    999
4     4     N    84     yellow   tom    112    136      126    122
5     5     P    56      brown   tom     64    230      150    130
6     6     P   105      green   dan    999    142      150    999
===================================================================

.Expected Image:
 ===================================================================
      ID   blood age  hair_color     name height weight distance expiry
    <dbl> <chr> <dbl>     <chr>     <chr>  <dbl>  <dbl>    <dbl>  <dbl>
1     1     P    60       yellow    john    999    128      110    120
2     4     N    84       yellow    tom     112    136      126    122
===================================================================

.Backgound:Beginner

Moverover, how to

1.filtering data with "contain" function? -like: extract data in column:hair_color ,contain "ye"

2.filtering data with multi conditions? -like: in column:hair_color ,contain "ye" and in column:name ,start with "to"

Simply imaging how to do something exactly like working in Excel, Thanks in advance.

rane
  • 901
  • 4
  • 12
  • 24
  • 1
    Possible duplicate of [Filtering a data.frame](https://stackoverflow.com/questions/1686569/filtering-a-data-frame) – Maurits Evers Nov 14 '17 at 03:39
  • Thank you very much . It works with using dplyr package --filter function. But i still unable to mix filter function with contain command. – rane Nov 14 '17 at 06:12
  • From the help, `contains` and related functions *"allow you to select variables based on their names"*. The key part is "select variables", not "filter rows". In a `data.frame`, a variable is a column. Use `grepl`, perhaps as `dplyr::filter(x, grepl("ye", hair_color))`. It also works just fine outside of `dplyr`, ala `x[grepl("ye", x$hair_color),]` or by using `subset`, as in KarstenW's answer (though `subset` warns against programmatic use). – r2evans Nov 14 '17 at 17:40

1 Answers1

0

In interactive mode, you could use subset:

1)

subset(x, grepl("ye", hair_color, fixed=TRUE))

2)

subset(x, grepl("ye", hair_color, fixed=TRUE) & grepl("^to", name))

In the second example the caret ^ means "start with" in regular expressions.

So in short, the simple text filtering functions known from excel can be achieved and much extended with grepl. A short course on regular expressions helps to unleash its real power.

Karsten W.
  • 17,826
  • 11
  • 69
  • 103