Search code examples
rif-statementreplacegrepl

R - Replacing values in dataframes with conditional statements and substrings


I have a dataframe df

enter image description here

In this is a large dataframe with hundreds of columns of data. In particular is 40 columns of data with similar names TD1, TD2 ..... TD40. I would like to scan each row and if any value begins with Z50 I would like the row value in the column Category to change to "Surgery". Hence in the above example rows 1 and 3 would convert from "Cardiac" to "Surgery".

In a similar example when the entire code was Z50 I used:

df %>%
rowwise()%>%
mutate(Category = if(any(c_across(starts_with("TD")) == "Z50")) 
                   "Surgery" else Category)

Now I just want the first 3 characters to equal Z50. I assume I would use a grepl function but not sure how to incorporate it into the original code. Well it hasn't work for me so far. Any ideas?. Thanks


Solution

  • We can try using apply and grepl for a base R option:

    idx_start <- grep("^TD1$", names(df))
    idx_end   <- grep("^TD40$", names(df))
    
    df$Category <- apply(df, 1, function(x) {
        ifelse(sum(grepl("^Z50", x[idx_start:idx_end])) > 0,
               "Surgery", df$Category)
    })