dimanche 24 octobre 2021

Select columns by pattern and replace factor at once

I have a data frame with several columns. Some of the columns have fixed patterns at the beginning for example q1a, q1a_30, q1a_60, q1a_90. I want to call the columns with the same begging pattern and replace their factors.

Process I can do it separately, for each stage but is there any way to do it at once? Here is as far what I have done:

df[,grepl("q1a", colnames(df))]
df$q1a<- recode_factor(df$q1a, `1` = "Yes", `2` = "No",`3` = "I don't know",`4` = "Maybe")

Aucun commentaire:

Enregistrer un commentaire