Character to numeric error

I have a dataframe with one column in character format:

"178,00", "191,00", "199,00", "205,00",  "234,00", "264,00",
# A tibble: 6 × 6
NAME    EMB_NAME.     DATE                  AG_TOTAL    AG_ACT        ELECTRIC_FLAG
  <chr>         <chr>          <dttm>              <chr>      <chr>                <dbl>
1 Brus - Tri    Bracara        1988-01-05 00:00:00 341,00     178,00                   0
2 Brus - Tri    Bracara        1988-01-12 00:00:00 341,00     191,00                   0
3 Brus - Tri    Bracara        1988-01-19 00:00:00 341,00     199,00                   0
4 Brus - Tri    Bracara        1988-01-26 00:00:00 341,00     205,00                   0
5 Brus - Tri    Bracara        1988-02-02 00:00:00 341,00     234,00                   0
6 Brus - Tri    Bracara        1988-02-09 00:00:00 341,00     264,00                   0

I try to convert column (AG_ACT) to number, with several methods:

as.numeric(as.character(df$AG_ACT)), df$AG_ACT  = mutate_if(is.character, as.numeric),
df$AG_ACT <- as.numeric(df$AG_ACT)

But I always end with this error:

Warning message:
NAs introduced by coercion 

Any idea what might be the problem?

# a normal number with a decimal point
"123.45" |> as.numeric()

# a number with comma used in place of a decimal point
"123,45" |> as.numeric()

# solution by substition
"123,45" |> gsub(pattern = ",",
                 replacement = ".") |> as.numeric()

#solution by helper functions
"123,45" |>  readr::parse_number(locale = locale(decimal_mark = ","))

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.