csik
csik

Reputation: 43

Convert character string into numeric values in R

I'm reading an .csv excel file in R and extracting the 5th column of my data. I get a character string back, but I need to convert the character string bmi2014 into numeric values without losing the information. This is what I have tried so far:

options(stringsAsFactors = FALSE)
setwd("~/CAD Project")
bmi <- read.csv("~/CAD Project/BMI sex and province.csv")
bmi <- bmi[8:46 , ] #removing rows I don't need
bmi <- bmi[, 2:6] #removing columns I don't need
bmi2014 <- bmi[, 5]
bmi2014
[1] "272,818"   "146,959"   "125,859"   "65,238"    "32,132"    "33,106"        
"443,317"   "234,307" "209,010"   "355,959"   "192,160"   "163,799"      
"3,226,705" "1,865,444" "1,361,261" "5,508,224" "3,133,853" "2,374,371"  
"533,910"   "296,162"   "237,748"   "446,312"   "254,005"   "192,307"  
[25] "1,658,172" "984,981"   "673,190"   "1,667,339" "990,920"   "676,418"        
"15,453"    "8,482" "6,971"     "19,607"    "11,312"    "8,294"     "9,469"           
"5,187"     "4,282"     
mydata <- as.numeric(as.character(bmi2014))
Warning message:
NAs introduced by coercion 

I've tried using type.convert and

 as.matrix(sapply(bmi2014, as.numeric), na.rm = TRUE) 

as well but can't seem to solve this problem as NA values are returned. What else can I try so that I have the list of numbers 272,818, 146,959, etc... Thanks!

Upvotes: 2

Views: 2309

Answers (1)

Pierre Lapointe
Pierre Lapointe

Reputation: 16277

The problem is the commas (,). You have to remove them with gsub before converting to numeric.

bmi2014 <-c("272,818","146,959","125,859","65,238", "32,132","33,106",
"443,317","234,307","209,010")
as.numeric(gsub(",","",bmi2014))
1[1] 272818 146959 125859  65238  32132  33106 443317 234307 209010

Upvotes: 2

Related Questions