rdatabase

prettyNum function in a dataframe with numerical and character columns


I have this sample dataframe

df <- data.frame(status=c('single', 'married', 'married', "fff"),
                 education=c(3456, 898989899, 23939004, NA),
                 income=c('34.5%', '88.456%', '92', NA))
df

I execute the following code to replace the decimal points with a comma in the character columns only

df1<-df %>% mutate(across(where(is.character), ~ str_replace_all(., "\\.", ",")))

The output df1 is OK. Next, I want to put dot ONLY to the values of the numerical columns. For example I want to have 3.456, 898.989.899, etc

But I have difficulty doing this. I tried

df2<- df1   %>% mutate(across(where(is.numeric) ,    ~   prettyNum(., big.mark=".")         ))

but although it seems to work, I get a warning message.

Warning message:
There were 5 warnings in `mutate()`.
The first warning was:
ℹ In argument: `across(where(is.numeric), ~prettyNum(., big.mark = "."))`.
Caused by warning in `prettyNum()`:
! 'big.mark' and 'decimal.mark' are both '.', which could be confusing
ℹ Run `dplyr::last_dplyr_warnings()` to see the 4 remaining warnings.

Also, in df2, I no longer have numeric columns, as indicated by str(df2). All columns are character columns which is something that I do not want, because NA becomes "NA", so in the excel instead of having blank cells, I will be having "NA" strings. I want to maintain the numerical class of the corresponding columns.

Is there a way to avoid these complications?

Note that my real dataset contains hundrends of such numeric and character columns and thousands of rows, so a more general code would help.


Solution

  • Ensure you indicate what the decimal mark should be:

    df1 %>% 
        mutate(across(where(is.numeric), ~prettyNum(., big.mark=".",decimal.mark = ',')))
    
       status   education  income
    1  single       3.456   34,5%
    2 married 898.989.899 88,456%
    3 married  23.939.004      92
    4     fff          NA    <NA>