1

I need to convert the Votes column variable on the following data frame from character to numeric without adding NA’s

 > res
                  Party              Votes                  
 1 Progressive Liberal Party         28,599           
 2    Free National Movement         19,781           
 3 Commonwealth Labour Party            254            
 4              Independents            753            
 5       Invalid/blank votes                             
 6                     Total         49,387            

 > str(res)
'data.frame':   7 obs. of  5 variables:
$ Party: chr  "Progressive Liberal Party" "Free National Movement" 
"Commonwealth Labour Party" "Independents" ...
$ Votes: chr  "28,599" "19,781" "254" "753" ...

I have found this post on StackOverflow with a number of suggestions and I tried the following methods

  • Using transform

    D <- transform(res, Votes = as.numeric(Votes))
    

    However, this leads to only a few numbers converting to numeric. See below

     1    NA
     2    NA
     3   254
     4   753
     5    NA
     6    NA
     7    NA 
    
  • using as.character and then using as.numeric

      as.numeric(as.character(res$Votes))
    

    But that leads to the same issue

    NA  NA 254 753  NA  NA  NA
    

How do I ensure that all numbers in the vote column are converted to numeric?

Nathgun
  • 115
  • 1
  • 1
  • 8

2 Answers2

3

To convert numbers that have commas, dollar signs, or similar formatting, use parse_number() from the readr package.

> library(readr)
> parse_number("28,599")
[1] 28599
Sam Firke
  • 21,571
  • 9
  • 87
  • 105
  • You can also just use `readr::read_csv` to import your CSV file in the first place, and avoid the whole problem. – Mike Stanley Sep 21 '17 at 19:01
1

The comma is throwing it off, you need to remove it first using gsub.

res$Votes <- as.numeric(gsub(",", "", res$Votes))
Eric Watt
  • 3,180
  • 9
  • 21