heres a dataframe:
# A B C D
# 0 loud one 0 0
# 1 quite one 1 2
# 2 silent two 2 4
# 3 loud three 3 6
# 4 quite two 4 8
# 5 silent two 5 10
and afterwards I want the dataframe to look like this:
# A B C D
# 0 loud one 0 0
# 1 quite one 1 2
# 2 loud three 3 6
# 3 quite two 4 8
How do I write a condition using dplyr extract or select functions where we use OR?.
eg. (dataframe is called volume) volume %<% filter(grepl(A, loud or quite))
CodePudding user response:
With dplyr filter
and str_detect
:
library(tidyverse)
tribble(
~A, ~B, ~C, ~D,
"loud", "one", 0, 0,
"quite", "one", 1, 2,
"silent", "two", 2, 4,
"loud", "three", 3, 6,
"quite", "two", 4, 8,
"silent", "two", 5, 10
) |>
filter(str_detect(A, "loud|quite"))
#> # A tibble: 4 × 4
#> A B C D
#> <chr> <chr> <dbl> <dbl>
#> 1 loud one 0 0
#> 2 quite one 1 2
#> 3 loud three 3 6
#> 4 quite two 4 8
Created on 2022-05-17 by the reprex package (v2.0.1)
CodePudding user response:
data.table
option:
library(data.table)
setDT(df)[A %in% c("loud", "quite")]
Output:
A B C D
1: loud one 0 0
2: quite one 1 2
3: loud three 3 6
4: quite two 4 8
Thanks to @Carl for creating the data!