Efficiently replacing multiple values in the same column + grouping values conditionally to one category

Dear community,

I would like to know how to replace multiple values in the same column. I have found different options but they look unnecessary complex.

Is there any way I can replace different values using this function?

data$fac[data$fac == "estuio"] <- "studio"

I tried this but it did not work:

data$fac[data$fac == "estuio", "estudy", "study"] <- "studio"

Also, I have another question related to that. I want to change multiple variables at the same time of the same column under a condition.

In my case, I have a variable with multiple categories. I want to stay with 4 categories and group all the other responses into a category called "other".

Do you have any advice on what function should I use? Again, I found some examples but I did not manage to run them correctly.

Use %in% instead of ==

data$fac[data$fac %in% c("estuio", "estudy", "study") ] <- "studio"

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

1 Like

That worked perfectly, thank you!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.