Filtering vectors in R

1 Motivation

We have a vector and we want to filter it by name.

2 Setup

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Demo vector:

v1 <- c(10, 20, 30)
v2 <- c("a", "b", "c", "a")
v3 <- c(a = "aaa", b = "bbbb", c = "cccc", d = "abcd")

3 Way 1: Base R

v1[v1 >= 10]
## [1] 10 20 30
v2[v2 %in% c("a", "b")]
## [1] "a" "b" "a"

4 Way 2: magrittr

library(magrittr)
## 
## Attaching package: 'magrittr'
## The following object is masked from 'package:purrr':
## 
##     set_names
## The following object is masked from 'package:tidyr':
## 
##     extract
v1 %>% 
  extract(c(1,2))  # by position
## [1] 10 20

5 Way 3: tidyverse

v2 %>% 
  str_detect("a")
## [1]  TRUE FALSE FALSE  TRUE
v2 %>% 
  str_subset("a")
## [1] "a" "a"
v3 %>% 
  str_subset("a")
## [1] "aaa"  "abcd"

6 Way 4: purrr

v2 %>% 
  keep(.p = ~str_detect(.x, "a"))
## [1] "a" "a"
v3 %>% 
  keep(.p = ~str_detect(.x, "a"))
##      a      d 
##  "aaa" "abcd"

7 Conclusions

Using base R is probably the most straight forward.