You can do something like this
library(tidyverse)
# Sample data on a copy/paste friendly format, replace with your actual data frame.
sample_df <- data.frame(
stringsAsFactors = FALSE,
Final.Test.Result = c(NA,"Discordant",
"HIV-1 Negative","HIV-1 Positive","HIV Negative",
"HIV Positive, undifferentiated","Inconclusive, further testing needed",
"Invalid","Negative","Positive","Preliminary positive"),
Count = c(328L,5L,88L,72L,5304L,1L,
30L,10L,37722L,110L,17L)
)
sample_df %>%
mutate(group = case_when(
str_detect(Final.Test.Result, "[Pp]ositive") ~ "Positive",
str_detect(Final.Test.Result, "[Nn]egative") ~ "Negative",
TRUE ~ "Other"
)) %>%
group_by(group) %>%
summarise(Count = sum(Count))
#> # A tibble: 3 x 2
#> group Count
#> <chr> <int>
#> 1 Negative 43114
#> 2 Other 373
#> 3 Positive 200
Created on 2021-03-26 by the reprex package (v1.0.0.9002)
Note: Next time please provide a proper REPRoducible EXample (reprex) illustrating your issue.