0

I would like to create a dichotomous variable about corruption defining:

0 = every country with a value inferior or equal to 50 (low level of corruption),

1 = every country with a value superior or equal to 50 (high level of corruption)

I don't know if i'm doing right

This is my desk:

My variable is corruption, so

cpi_nueva$corruption <- ifelse(cpi_nueva$corruption >= 50, "less corruption", "more corruption" )
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 1
    If you just need 0, 1 values, you can use `+(cpi_nueva$corruption >= 50)` – akrun Sep 02 '21 at 23:23
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 02 '21 at 23:31
  • What about now? – DaleCooper Sep 02 '21 at 23:38
  • This is just about basic R. I removed inadequate tags – GuedesBF Sep 02 '21 at 23:41
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. What's the problem with your current code? – MrFlick Sep 02 '21 at 23:42

1 Answers1

0

Here is a tidyverse solution, where:

  • cpi_nueva: is your data.frame
  • corruption: is the numeric variable in cpi_nueva
  • corruption_cat: is the dichotomous that will be created

Libraries

library(tidyverse)

Code

cpi_nueva %>% 
   mutate(
      corruption_cat  = if_else(corruption >= 50,"less corruption","high corruption")
   )
Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32