如何在 data.frame 中转换逗号中的点

How to convert dot in comma in data.frame

我想将点(小数点分隔符)转换为逗号作为小数点分隔符。 我尝试使用 format(decimal.mark=",") 但出现错误。

 df<-structure(list(ponto = c("F01", "F02", "F03", "F04", "F05", "F06"
 ), `Vegetação Nativa` = c(0.09, 3.12, 8.22, 5.92, 1.95, 4.7), 
     Agricultura = c(91.78, 91.87, 100, 100, 91.5, 99.38), Pastagem = c(-16.99, 
     -33.16, -22.73, -24.12, -38, -47.3), `Área Urbana` = c(27.32, 
     27.32, 27.57, 27.57, 19.18, NaN), `Solo Exposto` = c(10.04, 
     2.13, 8.5, 6.64, -29.35, -442.86), `Corpo Hídrico` = c(-15.62, 
     -15.62, NaN, NaN, -17.11, -25.93)), class = c("grouped_df", 
 "tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L), groups = structure(list(
     ponto = c("F01", "F02", "F03", "F04", "F05", "F06"), .rows = structure(list(
         1L, 2L, 3L, 4L, 5L, 6L), ptype = integer(0), class = c("vctrs_list_of", 
     "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
 ), row.names = c(NA, -6L), .drop = TRUE))

我试过了,但出现错误:

df%>%
  format(decimal.mark=",")

一种方法是使用 dplyr 中的 mutateacross。虽然这仍然会将它们的类型更改为 character.

library(dplyr)

df %>%
  mutate(across(everything(), format, decimal.mark = ","))

输出

# A tibble: 6 × 7
# Groups:   ponto [6]
  ponto `Vegetação Nativa` Agricultura Pastagem `Área Urbana` `Solo Exposto` `Corpo Hídrico`
  <chr> <chr>              <chr>       <chr>    <chr>         <chr>          <chr>          
1 F01   0,09               91,78       -16,99   27,32         10,04          -15,62         
2 F02   3,12               91,87       -33,16   27,32         2,13           -15,62         
3 F03   8,22               100         -22,73   27,57         8,5            NaN            
4 F04   5,92               100         -24,12   27,57         6,64           NaN            
5 F05   1,95               91,5        -38      19,18         -29,35         -17,11         
6 F06   4,7                99,38       -47,3    NaN           -442,86        -25,93 

此外,如果您想简单地更改在打印、绘图等时查看数据的方式,对于任何 as.character,您可以更改默认选项。您还可以阅读更多相关内容 here(这个 post 有很多与您的问题直接相关的讨论)。

options(OutDec= ",")  

示例(更改选项后):

c(1.5, 3.456, 40000.89)

# [1]     1,500     3,456 40000,890

但是,需要注意的是数据必须是字符。因此,对于您的数据,我们可以将它们转换为字符,然后它们将以逗号而不是句点显示。

df %>% mutate(across(everything(), as.character))

# A tibble: 6 × 7
# Groups:   ponto [6]
  ponto `Vegetação Nativa` Agricultura Pastagem `Área Urbana` `Solo Exposto` `Corpo Hídrico`
  <chr> <chr>              <chr>       <chr>    <chr>         <chr>          <chr>          
1 F01   0,09               91,78       -16,99   27,32         10,04          -15,62         
2 F02   3,12               91,87       -33,16   27,32         2,13           -15,62         
3 F03   8,22               100         -22,73   27,57         8,5            NaN            
4 F04   5,92               100         -24,12   27,57         6,64           NaN            
5 F05   1,95               91,5        -38      19,18         -29,35         -17,11         
6 F06   4,7                99,38       -47,3    NaN           -442,86        -25,93