I have a dataframe df
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

Aucun commentaire:
Enregistrer un commentaire