0

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?

enter image description here

Feverish123
  • 105
  • 1
  • 7
  • 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 Answers1

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