Mutate with Ifelse

Hello All,

I am in need of your help. It is probably something small I am missing, but I am trying to take percentages in a column and mutate those into a new column and then use ifelse to put the percentages into categories such as good or bad.

The column is titled PovertyLev and uses percentages to gauge a persons poverty level

example = 76%

I want to create a new column and categorize poverty rates.

Do I need to convert and do something before I can categorize everything correctly?

Here is the coding structure I am using:

DF <- MASTER.SERVICE.REPORT.(3)%>%
mutate(Eligability2 = if_else(PovertyLev >200%, "Good", "Bad"))

However, when I use the coding structure shown above, I get this error in R

Error: unexpected input in:
"DF <- MASTER.SERVICE.REPORT.(3)%>%
mutate(Eligability2 = if_else(PovertyLev > 200%, "Good", "Bad"))"

All the help is greatly appreciated.

Thank you,

That is a logical operation, greater than, being performed on a string, "200%". You want PovertyLev to be numeric, 200.

Would I just use as.numeric to convert the povertyLev column to whole numbers?

If so, how would I structure everything?

Thank you for your help.

If you have a choice, code the data so that percentages are represented by decimals.

PovertyLev <- c(.52,46,32,12,76)

If you are stuck with

PovertyLev <- c("52%","46%","32%","12%","76%")

use

PovertyLev <- c("52%","46%","32%","12%","76%")
as.numeric(gsub("%","",PovertyLev))/100
#> [1] 0.52 0.46 0.32 0.12 0.76

Wait until preparing a presentation table to format as a percentage.

PovertyLev <- c("52%","46%","32%","12%","76%")
scales::label_percent()(as.numeric(gsub("%","",PovertyLev))/100)
#> [1] "52.0%" "46.0%" "32.0%" "12.0%" "76.0%"

For more questions, see the FAQ: How to do a minimal reproducible example reprex for beginners.

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.