0

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?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Thomasiko
  • 39
  • 4
  • 1
    Does this answer your question? [Format number in R with both comma thousands separator and specified decimals](https://stackoverflow.com/questions/29465941/format-number-in-r-with-both-comma-thousands-separator-and-specified-decimals) – Limey Nov 29 '22 at 07:35
  • When importing the data set the `dec` decimal argument to ",". – zx8754 Nov 29 '22 at 08:40

1 Answers1

2

We can swap the comma for a dot:

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

[1] 3021.43
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360