Count a string and sum them up in row

I want to count how many "X" are there in each row and save the result in a new variable called OUTPUT.

I'd like to get the desired output using tidyverse. I have my final output as shown in the attachment.

I can easily do this in excel using countif function.

Any help will be much appreciated. Thank you

sample2 <- tibble::tribble(
   ~A,  ~B,  ~C,  ~D, ~OUTPUT,
  "1", "X",  NA, "X",      2L,
  "X", "X", "X", "X",      4L,
  "X",  NA,  NA, "1",      1L
  )
dplyr::mutate(sample2, OUTPUT2 = rowSums(sample2 == "X", na.rm = TRUE))
3 Likes

Thank you @hughparsonage for the solution.