JiaxiangBU / tutoring2

The collection of Python and R code scripts to tutor others.
https://jiaxiangbu.github.io/tutoring2/
Other
8 stars 7 forks source link

针对tibble中的数值型数据,如何自定义保留小数位数? #33

Closed slsongge closed 4 years ago

slsongge commented 4 years ago

问题

下图中红框内的数值如何自定义保留小数点位数? image

数据

df_01_2.zip

代码

library(plyr)
library(tidyverse)

df_01_2 <- read_csv("df_01_2.csv")

df_01_2 %>% 
  filter(!g4reading %in% NA) %>%
  group_by(kinder) %>%
  summarise_at(
    vars(g4reading), 
    list(~min(.), ~quantile(., 0.25), ~median(.), ~mean(.), ~quantile(., 0.75), ~max(.))
  )

我当前的解决方法

df_01_2 %>% 
  filter(!g4reading %in% NA) %>%
  group_by(kinder) %>%
  summarise_at(
    vars(g4reading), 
    list(~min(.), ~quantile(., 0.25), ~median(.), ~mean(.), ~quantile(., 0.75), ~max(.))
  ) %>%
  data.frame() %>%
  mutate_if(is.numeric, ~round(., 2)) %>%
  dplyr::rename(qu_1st = quantile..2, qu_3rd = quantile..5)

我的解决方法返回的结果如下图,其中红框的内容不知道为啥保留了一位小数? image

JiaxiangBU commented 4 years ago

@slsongge

$ unzip data/df_01_2.zip
Archive:  data/df_01_2.zip
  inflating: df_01_2.csv

下图中红框内的数值如何自定义保留小数点位数?

library(tidyverse)
df_01_2 <- read_csv(here::here("jinxiaosong/data/df_01_2.csv"))
## Parsed with column specification:
## cols(
##   race = col_character(),
##   classtype = col_double(),
##   yearssmall = col_double(),
##   hsgrad = col_double(),
##   g4math = col_double(),
##   g4reading = col_double(),
##   kinder = col_character()
## )
df_01_2 %>% head() %>% 
    knitr::kable("markdown")
race classtype yearssmall hsgrad g4math g4reading kinder
white 3 0 NA NA NA regular with aid
black 3 0 NA 706 661 regular with aid
white 3 0 1 711 750 regular with aid
black 1 4 NA 672 659 small
white 2 0 NA NA NA regular
white 3 0 NA NA NA regular with aid
df_01_2 %>% filter(is.na(g4reading)) %>% head() %>% 
    knitr::kable("markdown")
race classtype yearssmall hsgrad g4math g4reading kinder
white 3 0 NA NA NA regular with aid
white 2 0 NA NA NA regular
white 3 0 NA NA NA regular with aid
white 3 0 NA NA NA regular with aid
white 3 0 NA NA NA regular with aid
white 3 0 NA NA NA regular with aid
df <-
    df_01_2 %>%
    filter(!is.na(g4reading)) %>%
    group_by(kinder) %>%
    summarise_at(vars(g4reading),
                 list(
                     ~ min(.),
                     ~ quantile(., 0.25),
                     ~ median(.),
                     ~ mean(.),
                     ~ quantile(., 0.75),
                     ~ max(.)
                 ))
df %>% head()
## # A tibble: 3 x 7
##   kinder             min quantile..2 median  mean quantile..5   max
##   <chr>            <dbl>       <dbl>  <dbl> <dbl>       <dbl> <dbl>
## 1 regular            528        693     723  720.        749.   836
## 2 regular with aid   528        698.    722  721.        750    836
## 3 small              528        697     724  723.        750    836

有两种方法。

df %>% 
    mutate_if(is.numeric, scales::number, accuracy=1) %>% 
    knitr::kable("markdown")
kinder min quantile..2 median mean quantile..5 max
regular 528 693 723 720 749 836
regular with aid 528 698 722 721 750 836
small 528 697 724 723 750 836
# 精确到个位
df %>% 
    mutate_if(is.numeric, scales::number, accuracy=.1) %>% 
    knitr::kable("markdown")
kinder min quantile..2 median mean quantile..5 max
regular 528.0 693.0 723.0 719.9 749.2 836.0
regular with aid 528.0 697.5 722.0 720.7 750.0 836.0
small 528.0 697.0 724.0 723.4 750.0 836.0
# 精确到小数点一位
df %>% 
    mutate_if(is.numeric, scales::number, accuracy=.01) %>% 
    knitr::kable("markdown")
kinder min quantile..2 median mean quantile..5 max
regular 528.00 693.00 723.00 719.89 749.25 836.00
regular with aid 528.00 697.50 722.00 720.72 750.00 836.00
small 528.00 697.00 724.00 723.39 750.00 836.00
# 精确到小数点二位
df %>% 
    mutate_if(is.numeric, scales::number, accuracy=10) %>% 
    knitr::kable("markdown")
kinder min quantile..2 median mean quantile..5 max
regular 530 690 720 720 750 840
regular with aid 530 700 720 720 750 840
small 530 700 720 720 750 840
# 精确到十位

这个包开发的时候是为了给 ggplot2 搞单位的。

下面这个是任坤大神的。

df %>% 
    mutate_if(is.numeric, formattable::digits, digits=2) %>% 
    knitr::kable("markdown")
kinder min quantile..2 median mean quantile..5 max
regular 528.00 693.00 723.00 719.89 749.25 836.00
regular with aid 528.00 697.50 722.00 720.72 750.00 836.00
small 528.00 697.00 724.00 723.39 750.00 836.00
# 小数点后两位

最后是 base 的方法。

for (i in 1:10) {
    print(pi, digits = i)        
}
## [1] 3
## [1] 3.1
## [1] 3.14
## [1] 3.142
## [1] 3.1416
## [1] 3.14159
## [1] 3.141593
## [1] 3.1415927
## [1] 3.14159265
## [1] 3.141592654
JiaxiangBU commented 4 years ago

这个和

最后是 base 的方法。

for (i in 1:10) {
    print(pi, digits = i)        
}
## [1] 3
## [1] 3.1
## [1] 3.14
## [1] 3.142
## [1] 3.1416
## [1] 3.14159
## [1] 3.141593
## [1] 3.1415927
## [1] 3.14159265
## [1] 3.141592654

类似,只不过是在options(digits=n)设置。 参考 http://brucezhaor.github.io/blog/2016/05/05/read-data-1/ @slsongge