Home > database >  Change a number with comma, which is a char variable, to a numerical
Change a number with comma, which is a char variable, to a numerical

Time:11-29

I have some data containing a column named "amount" which look at individual transactions. I have exported a csv file from our database to excel, then uploaded the document into RStudio. When I load the file into my enviroment the "amount" column is formated like (ex. "3021,43"). Instead, I am attempting to make that column to be formatted like 3021.43.

When I use:

MD$Belop <- as.numeric(as.character(MD$Belop))

All the values containing a comma is returned with NA. Any tips?

CodePudding user response:

We can swap the comma for a dot:

x <- "3021,43"
num <- as.numeric(sub(",", ".", x, fixed=TRUE))
num

[1] 3021.43
  • Related