Reputation: 125
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?
Upvotes: 1
Views: 196
Reputation: 23014
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
Upvotes: 3
Reputation: 3230
The comma is throwing it off, you need to remove it first using gsub
.
res$Votes <- as.numeric(gsub(",", "", res$Votes))
Upvotes: 1