I think this is a rather simple question which nevertheless frustrates me very much because all my attempts do not work:
I have a dataset(df) where an important column is defined as a character. To make matters worse, the numbers are represented with a comma, although all decimal places are 0.
Product | Amount |
---|---|
12333 | 5,0 |
12333 | 1,0 |
What I tried:
mutate(df$Amount = as.numeric(df,Amount))
Which gave me an error (Error: unexpected(s) '=' in "mutate(df$Amount =")
I also tried
as.numeric(df$Amount)
Which gave me a warning (NAs created by conversion) and thus created only NAs
Can someone please explain to me where I made a mistake and how I can do it the right way?
In addition: I joined Stackoverflow recently, so if I asked the question wrong or didn't provide valueable informations to my problem, please let me know.
CodePudding user response:
You can do:
library(stringr)
x <- c('5', '5.0', '5,0')
as.numeric(str_replace(x, ',', '.'))
which gives:
[1] 5 5 5
In your case:
df$Amount <- as.numeric(str_replace(df$Amount, ',', '.'))
Your problem was that you have a comma as decimal point but it needs to be a point.