R problem assigning multiple string values from a tibble to a variable

Viewed 78

I am trying to extract multiple different string values from a single column and assign them to a new variable. I tried the following:

variable = filter(tibble, column_name == "string1" & column_name == "string2" & column_name == "string3")

This fails. I have tried similar methods, but I cannot find a method that allows multiple strings to be held in the same container. I am using the tidyverse library.

1 Answers

It is because of the &. It should be | as no two elements can occur at the same time.

library(dplyr)
filter(tibble,column_name == "string1"| column_name =="string2"| column_name =="string3")

Or instead of ==, use %in%

filter(tibble, column_name %in% c('string1', 'string2', 'string3'))
Related