Hi Posit Community.
Hopefully quick question here.
Trying to filter a dataframe by a list of strings found at the beginning of each value in a column, but I'm not sure what the most efficient way to do this would be or how to deploy it across a list.
As an example, lets say I have the following:
Participant | Category | Rating |
---|---|---|
Greg | F0 | 21 |
Greg | C0.0 | NA |
Donna | 1 | 17 |
Donna | 01 | 21 |
df <- data.frame(
Participant = c('Greg', 'Greg', 'Donna', 'Donna'),
Category = c('F0', 'C0.1', '1', '01'),
Rating = c(21, NA, 17, NA))
But I only want to retain rows that begin with the following strings:
filterlist <- c("F","C","1")
Like so:
Participant | Category | Rating |
---|---|---|
Greg | F0 | 21 |
Greg | C0.0 | NA |
Donna | 1 | 17 |
I'm unfortunately drawing a blank on how best to implement this.
The following do nothing:
> df|> filter(Category %in% paste0("^",filterlist))
[1] Participant Category Rating
<0 rows> (or 0-length row.names)
> df |> filter(Category == paste(paste0("^",filterlist), collapse="|"))
[1] Participant Category Rating
<0 rows> (or 0-length row.names)
> df |> filter(Category == stringr::str_starts(Category, filterlist))
Error in `filter()`:
ℹ In argument: `Category == stringr::str_starts(Category, filterlist)`.
Caused by error in `stringr::str_starts()`:
! Can't recycle `string` (size 4) to match `pattern` (size 3).
Run `rlang::last_trace()` to see where the error occurred.
Would anyone happen to know what I'm missing here?
Thank you in advance!