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.
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))