R 具体的小数位数取决于数字的大小

R 具体的小数位数取决于数字的大小,r,tidyverse,R,Tidyverse,我试图显示我的数据集的“有效数字”。我有一个数据集,有高数字和低数字,我想在数据表中显示(这里的数据表,我的意思是,一篇论文中的一个数字,而不是R中的数据表(DT))。我不想定在小数的固定数目上。我想要大于10=0的小数,介于10到>0=1的小数,0到0.1=2的小数,0.1到>0.01=3的小数,你的df是 > df # A tibble: 30 x 3 i.x33.031 i.x35.034 days_incubated <dbl> <db

我试图显示我的数据集的“有效数字”。我有一个数据集,有高数字和低数字,我想在数据表中显示(这里的数据表,我的意思是,一篇论文中的一个数字,而不是R中的数据表(DT))。我不想定在小数的固定数目上。我想要大于10=0的小数,介于10到>0=1的小数,0到0.1=2的小数,0.1到>0.01=3的小数,你的df是

> df
# A tibble: 30 x 3
   i.x33.031 i.x35.034 days_incubated
       <dbl>     <dbl>          <dbl>
 1     13.6     0.123               4
 2      0       0                   4
 3    105.      0.0351              4
 4     29.0     0.0407              4
 5     26.1     0.0497              4
 6      4.11    0.0153              4
 7     62.1     0.0856              4
 8    137.      0.230               4
 9      0       0                   4
10    292.      0.386               4
# ... with 20 more rows
>df
#一个tibble:30x3
i、 x33.031 i.x35.034天
1     13.6     0.123               4
2      0       0                   4
3    105.      零点零三五一四
4     29.0     0.0407              4
5     26.1     0.0497              4
6      4.11    0.0153              4
7     62.1     0.0856              4
8    137.      零点二三零四
9      0       0                   4
10    292.      零点三八六四
# ... 还有20行
仅用于四舍五入

df %>% mutate(i.x33.031 = case_when(i.x33.031 > 10 ~ round(i.x33.031),
                                    i.x33.031 > 0 ~ round(i.x33.031, 1),
                                    i.x33.031 > 0.1 ~ round(i.x33.031, 2),
                                    i.x33.031 > 0.01 ~ round(i.x33.031, 3),
                                    TRUE ~ round(i.x33.031, 4)))
# A tibble: 30 x 3
   i.x33.031 i.x35.034 days_incubated
       <dbl>     <dbl>          <dbl>
 1      14      0.123               4
 2       0      0                   4
 3     105      0.0351              4
 4      29      0.0407              4
 5      26      0.0497              4
 6       4.1    0.0153              4
 7      62      0.0856              4
 8     137      0.230               4
 9       0      0                   4
10     292      0.386               4
# ... with 20 more rows
df%>%突变(i.x33.031=情况_,当(i.x33.031>10~轮(i.x33.031),
i、 x33.031>0~圆形(i.x33.031,1),
i、 x33.031>0.1~圆形(i.x33.031,2),
i、 x33.031>0.01~圆形(i.x33.031,3),
真圆形(i.x33.031,4)))
#一个tibble:30x3
i、 x33.031 i.x35.034天
1      14      0.123               4
2       0      0                   4
3     105      0.0351              4
4      29      0.0407              4
5      26      0.0497              4
6       4.1    0.0153              4
7      62      0.0856              4
8     137      0.230               4
9       0      0                   4
10     292      0.386               4
# ... 还有20行
两列四舍五入

result <- df %>% mutate_at(.funs = ~ case_when(. > 10 ~ round(.),
                                    . > 0 ~ round(., 1),
                                    . > 0.1 ~ round(., 2),
                                    . > 0.01 ~ round(., 3),
                                    TRUE ~ round(., 4)), .vars = 1:2)

> print(result)
# A tibble: 30 x 3
   i.x33.031 i.x35.034 days_incubated
       <dbl>     <dbl>          <dbl>
 1      14         0.1              4
 2       0         0                4
 3     105         0                4
 4      29         0                4
 5      26         0                4
 6       4.1       0                4
 7      62         0.1              4
 8     137         0.2              4
 9       0         0                4
10     292         0.4              4
# ... with 20 more rows
result%mutate_at(.funs=~case_当(.>10~轮(.),
.>0轮(,1),
.>0.1~圆形(,2),
.>0.01~圆形(,3),
真~四舍五入(,4)),.vars=1:2)
>打印(结果)
#一个tibble:30x3
i、 x33.031 i.x35.034天
1      14         0.1              4
2       0         0                4
3     105         0                4
4      29         0                4
5      26         0                4
6       4.1       0                4
7      62         0.1              4
8     137         0.2              4
9       0         0                4
10     292         0.4              4
# ... 还有20行
编辑用于打印和舍入

df %>% mutate_at(.funs = ~ case_when(. == 0 ~ formatC(0, format = "f", digits = 0),
                                     . < 0.01 ~ formatC(round(., 4), format = "f", digits = 4),
                                     . < 0.1 ~ formatC(round(., 3), format = "f", digits = 3),
                                     . < 1 ~ formatC(round(., 2), format = "f", digits = 2),
                                     . < 10 ~ formatC(round(., 1), format = "f", digits = 1),
                                     . >= 10 ~ formatC(round(., 0), format = "f", digits =0)), .vars = 1:2)

# A tibble: 30 x 3
   i.x33.031 i.x35.034 days_incubated
   <chr>     <chr>              <dbl>
 1 14        0.12                   4
 2 0         0                      4
 3 105       0.035                  4
 4 29        0.041                  4
 5 26        0.050                  4
 6 4.1       0.015                  4
 7 62        0.086                  4
 8 137       0.23                   4
 9 0         0                      4
10 292       0.39                   4
# ... with 20 more rows
df%>%mutate_at(.funs=~case_当(.==0~formatC(0,format=“f”,digits=0),
.<0.01~格式C(四舍五入,格式为“f”,数字为4),
.<0.1~格式C(四舍五入(,3),格式=“f”,数字=3),
.<1~格式C(四舍五入(,2),格式=“f”,数字=2),
.<10~格式C(四舍五入(,1),格式=“f”,数字=1),
.>=10~formatC(四舍五入(,0),format=“f”,digits=0)),.vars=1:2)
#一个tibble:30x3
i、 x33.031 i.x35.034天
1 14        0.12                   4
2 0         0                      4
3 105       0.035                  4
4 29        0.041                  4
5 26        0.050                  4
6 4.1       0.015                  4
7 62        0.086                  4
8 137       0.23                   4
9 0         0                      4
10 292       0.39                   4
# ... 还有20行

注意
在最后一段管道/语法中,即在打印之前,建议使用
formatC
将所有
双值
转换为
字符

您想要的输出是什么?您好@DPH,我已经详细阐述了我的问题,你不能写一些if语句来创建表吗?对于列表中的数字:如果数字==(10^1)数字=数字可能是四舍五入的,但我不太确定如何做到这一点是的,确切地说是@AnilGoyal,谢谢"! 然后我只需要将它应用于许多列,最好是基于它们的列位置。我可能做错了什么,但是我得到的结果是所有数字都有一个小数点。“.vars=1:2)指的是应该舍入的列号吗?.vars表示列号是的。可能您正在关注舍入以及结果的打印。请尝试代码,我在回答中添加了此代码
result <- df %>% mutate_at(.funs = ~ case_when(. > 10 ~ round(.),
                                    . > 0 ~ round(., 1),
                                    . > 0.1 ~ round(., 2),
                                    . > 0.01 ~ round(., 3),
                                    TRUE ~ round(., 4)), .vars = 1:2)

> print(result)
# A tibble: 30 x 3
   i.x33.031 i.x35.034 days_incubated
       <dbl>     <dbl>          <dbl>
 1      14         0.1              4
 2       0         0                4
 3     105         0                4
 4      29         0                4
 5      26         0                4
 6       4.1       0                4
 7      62         0.1              4
 8     137         0.2              4
 9       0         0                4
10     292         0.4              4
# ... with 20 more rows
df %>% mutate_at(.funs = ~ case_when(. == 0 ~ formatC(0, format = "f", digits = 0),
                                     . < 0.01 ~ formatC(round(., 4), format = "f", digits = 4),
                                     . < 0.1 ~ formatC(round(., 3), format = "f", digits = 3),
                                     . < 1 ~ formatC(round(., 2), format = "f", digits = 2),
                                     . < 10 ~ formatC(round(., 1), format = "f", digits = 1),
                                     . >= 10 ~ formatC(round(., 0), format = "f", digits =0)), .vars = 1:2)

# A tibble: 30 x 3
   i.x33.031 i.x35.034 days_incubated
   <chr>     <chr>              <dbl>
 1 14        0.12                   4
 2 0         0                      4
 3 105       0.035                  4
 4 29        0.041                  4
 5 26        0.050                  4
 6 4.1       0.015                  4
 7 62        0.086                  4
 8 137       0.23                   4
 9 0         0                      4
10 292       0.39                   4
# ... with 20 more rows