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 usingas.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?