Home > other >  Change dot (.) to comma (,) in R
Change dot (.) to comma (,) in R

Time:04-07

I have a dataset I am working on in R, however, one of the column values has dot (.) instead of a comma (,) so I think this might be messing up when I am running the regression. Does anyone know what code I should run to change all the dots to commas?

Thanks beforehand.

CodePudding user response:

Assuming you have a dataframe named df

df %>% mutate_all(funs(str_replace(., "\\.", ",")))

If its for one column only

df %>%  mutate(col1 = gsub("\\.", ",", col1))

CodePudding user response:

Assuming your data is a character vector inside a dataframe

df <- data.frame(var = c("5.1", "30", "..", "75.234.4423.5"))

With gsub

df$var <-  gsub("\\.", ",", df$var)

With stringi and purrr

library(stringi)
library(purrr)

df$var <- modify_if(df$var, stri_detect_fixed(df$var, "."), 
                    ~stri_sub_replace_all(., stri_locate_all_fixed(., "."), replacement=","))

Output

df

            var
1           5,1
2            30
3            ,,
4 75,234,4423,5

I used purrr::modify_if with the predicate stri_detect_fixed(df$var, ".") so that the values without any dots (in my example, 30) are not converted to NA by stringi::stri_sub_replace_all. The stringi version is more flexible for other purposes, you can pass functions inside the replacement argument when you want a dynamic replacement value.

I cannot comment on if it will help your regression analysis. I simply answered by giving ways to change dots to commas in a character vector.

  • Related