Trouble converting factor to numeric

I'm trying to run a simple summerise function, however I get the error message:

Problem with summarise() input TotalAmount.
x ‘sum’ not meaningful for factors
i Input TotalAmount is sum(Amount).

I tried using as.numeric() function and the as.numeric(as.character()) both times it has resulted in error messages


To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Here's the sample of what I'm trying to do:

Code Amount
001 78,382
001 89,768
007 56,621
006 52,921
002 38,922
006 36,889
006 87,373

I want to group the data set by code, then add up the amount. However R is reading the Amount values as a factor rather than numeric. I have over 3,000 rows with 5 values

It would be even better if you read the information in the link and acted on it, but on this occasion, I can at least diagnose your problem immediately. You have commas in the character string representation of Amount. as.numeric won't know what to do with that.

You have two options.

  1. use string functions to edit out the commas, perhaps base::gsub()
  2. use a function that is designed to interpret numbers from text, perhaps readr::parse_number()

in both cases you want to convert from the factors you have to strings first, with as.character()

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.