0

I have a column of salaries in R. They are salaries with 2 decimal points. When I read them in as a CSV, R recognizes the column as Factors. I can't perform functions such as sum or subset to analyze the data. Thus, I changed the column to numeric with the command:

payroll$Total.Earnings <- as.numeric(payroll$Total.Earnings)

However, this drastically changes the amounts of the salaries. For example, a person who made $100381.19 is changed to 86 after I write the command above. What am I doing incorrectly?

Jaap
  • 81,064
  • 34
  • 182
  • 193
George
  • 317
  • 2
  • 4
  • 16
  • 1
    Use `payroll$Total.Earnings <- as.numeric(as.character(payroll$Total.Earnings))` instead – maccruiskeen Oct 15 '15 at 14:41
  • when I do payroll$Total.Earnings <- as.numeric(as.character(payroll$Total.Earnings)) I get NAs. The warning message says "NAs introduced by coercion " – George Oct 15 '15 at 15:00
  • Do you have the dollar `$` sign in your salary values? If that is the case, you have to remove it, because R doesn't know that `$` means dollar (it's just a character like any other). So, if that is the case, you need to get rid of the dollar sign, before converting it to numeric. You can, for example, use the `sub()` function like so: `as.numeric(sub("\\$", "", as.character(payroll$Total.Earnings)))`. – hugot Oct 15 '15 at 15:11

0 Answers0