Based on the table below, how would I be able to put "wedding" and "renewable_energy" which both occur less frequently, into the "other" category?
Asked
Active
Viewed 32 times
0
-
Using ifelse: `loans$reason <- ifelse(loans$reason %in% c("wedding", "renewable_energy"), "other", loans$reason)` – zx8754 Mar 09 '21 at 12:30
-
That changed the values to numbers for some reason, but it kept other – Feverish123 Mar 09 '21 at 12:38
-
Provide example data, and expected output. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – zx8754 Mar 09 '21 at 12:41
1 Answers
1
loans$reason <- factor(ifelse(as.character(loans$reason) %in% c("wedding", "renewable_energy"), "other", loans$reason))
Your reason column is probably of type factor, hence it changed to numbers, so you need to convert to character first. And optionally, after that to factor again.

Lennyy
- 5,932
- 2
- 10
- 23
-
That still replaces the variables with numbers for some reason. Any ideas why? – Feverish123 Mar 09 '21 at 22:38
-
Then please add sample data and expected output as @zx8754 mentioned so we can look into it. – Lennyy Mar 10 '21 at 06:05