How to set consistent decimal separators in R data frame?

130 Views Asked by At

Some columns in my data frame have ',' and some have '.' as decimal separator.

DateTime             a    b    c
2000-01-01 00:30:00  3,4  6,1  9.5
2000-01-01 00:45:00  3,5  6,8  7.4

I would like to have '.' as a decimal separator throughout my data frame.

I have tried

df = gsub(',','.',df)

but it just gives me a new Value instead of a data frame. I also tried

df = lapply(df, function(x) as.character(gsub(",",".",df)))

but it just makes a list.

I have tried to set the columns as numeric (currently they're all characters due to earlier procedures), but it sets all values in a and b as NA.

2

There are 2 best solutions below

4
Chris Ruehlemann On BEST ANSWER

You can replace , with . in all columns like this:

library(tidyverse)
df %>%
  mutate(across(everything(), ~str_replace(., ",", ".")))

If you have columns where , should not be replaced you can address the ones where the change should be implemented more specifically, e.g., like this:

library(tidyverse)
df %>%
  mutate(across(a:c, ~str_replace(., ",", ".")))

Data:

df <- data.frame(
  DateTime = c(2000-01-01, 2000-01-01),
  a = c("3,4","3.5"),
  b = c("6,1", "6,8"),
  c = c(9.5, 7.4))
0
dufei On

Probably the numbers with a comma as decimal separator are character strings. Convert them to numeric values like so:

library(readr)
parse_number("3,4", locale = locale(decimal_mark = ","))
#> [1] 3.4

Created on 2023-03-20 with reprex v2.0.2