Saibal Dutta
Saibal Dutta

Reputation: 23

Converting character to numeric in R

I Have dataset like below which I am trying to convert column "Installs" to numeric, my codes are like below:

Original Dataset

My Codes:-

Data$Installs<-substr(Data$Installs,1,nchar(Data$Installs)-1)
Data$Installs<-gsub(",","",gsub("\\s+","",Data$Installs))
Data$Installs<-as.numeric(Data$Installs)

after the code I get below

This is the result I get Any help?

Upvotes: 0

Views: 191

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520898

From what I can see, you need only to remove commas and a possible trailing plus sign. So, the following should work:

Data$Installs <- as.numeric(gsub("[+,]", "", Data$Installs))

You might want to create a new column though and keep the original one.

Upvotes: 1

Related Questions