0

I know that variations of this question have been asked and answered, but I can't seem to get any to work for me.

I have a column labeled 'Interest.Rate', and the values are of this form: 12.75%

I tried:

as.numeric(as.character(loansData$Interest.Rate))

And what I got back was a screen full of 'NA's.

I suspect that I need to remove the '%' sign, but I'm not sure.

Any suggestions?

1 Answer 1

5

You're right, you need to remove the '%' sign and then coerce the result to be numeric

> set.seed(1)
> Interest.Rate <- as.factor(paste0(sample(10), "%"))
> as.numeric(as.character(Interest.Rate))  # problem!!
 [1] NA NA NA NA NA NA NA NA NA NA

> as.numeric(gsub("\\%", "", Interest.Rate))  # solution
 [1]  3  4  5  7  2  8  9  6 10  1
Sign up to request clarification or add additional context in comments.

3 Comments

Same tool, different flavor: loansData$Interest.Rate <- as.numeric(gsub("[^0-9]", "",loansData$Interest.Rate))
@ChelseaE, not if there are decimals as in the original post. Your "flavor" would strip the decimals out.
Woops! You are completely correct @AnandaMahto, the right code should read loansData$Interest.Rate <- as.numeric(gsub("[^0-9\\.]", "",loansData$Interest.Rate)), thank you for your correction!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.