Filter rows based on condition

Hi all, below is the dataframe df. Can we filter it based on conditions. Like, if there are charaters in 3rd col and Null in 3rd col, those should be filtered

  row col value
1   1   0    ID
2   1   1    12
3   1   2    12
4   1   3     4
5   1   4      

Expected output

  row col value
2   1   1    12
3   1   2    12
4   1   3     4

This can be easily accomplished by using the filter() verb from dplyr. You can read more about it here: https://dplyr.tidyverse.org/reference/filter.html

For pattern matching, you can use regular expressions (regex for short). Learn more about them here: https://regexone.com/

library(dplyr, warn.conflicts = FALSE)
#> Warning: package 'dplyr' was built under R version 3.6.3
library(stringr, warn.conflicts = FALSE)

df <- tribble(~ row, ~ col, ~ value,
              1, 0, "ID",
              1, 1, 12,
              1, 2, 12,
              1, 3, 4,
              1, 4, NA)

filter(df, str_detect(value, "[0-9]+"))
#> # A tibble: 3 x 3
#>     row   col value
#>   <dbl> <dbl> <chr>
#> 1     1     1 12   
#> 2     1     2 12   
#> 3     1     3 4

Created on 2020-03-30 by the reprex package (v0.3.0)

Thanks a lot.............

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.