How to count string numbers row-wisely and count percentage (only not empty columns taking into account)

You can use -

#To select only required columns
tmp <- df[-1]
#Total true values divided by total values which are not `NA` or empty.
df$true_pct <- rowSums(tmp == "true")/rowSums(tmp != "" & !is.na(tmp))
df

#  test  2019  2020  2021  2022  true_pct
#1    A  true false  true       0.6666667
#2    B        true false false 0.3333333
#3    C false  true  true false 0.5000000

How about this with rowSums and na.rm = TRUE:

df$true_pct <- rowSums(df[-1] == "true", na.rm = TRUE)/rowSums(df[-1] != "" & !is.na(df[-1]), na.rm = TRUE)

or this dplyrsolution with apply:

library(dplyr)
library(stringr)
    df %>%
      mutate(true_pct = apply(.[-1], 1, paste, collapse = " "),
             true_pct = str_count(true_pct, "true")/str_count(true_pct, "true|false"))
  test 2018  2019  2020  2021  2022  true_pct
1    A true  true false  true       0.7500000
2    B <NA>        true false false 0.3333333
3    C <NA> false  true  true false 0.5000000

Data:

df <- structure(list(test = c("A", "B", "C"), `2018` = c("true", NA, NA
), `2019` = c("true", "", "false"), `2020` = c("false", "true", 
                                               "true"), `2021` = c("true", "false", "true"), `2022` = c("", 
                                                                                                        "false", "false")), class = "data.frame", row.names = c(NA, -3L
                                                                                                        ))