簡體   English   中英

r:頭數據時取整

[英]r: rounding when head data

我無法弄清楚如何使r正確顯示數據的開頭。 這是Swirl的一項任務,因此我必須弄清楚如何以Swirl會接受的方式編寫代碼。
Swirl希望最終的打印輸出看起來像這樣:

## Pclass   agecat    Sex      N     survivors   perc_survived
## <int>   <fctr>    <chr>   <int>     <int>         <dbl>
##   1    Under 15  female     2         1        50.000000
##   1    Under 15    male     3         3       100.000000
##   1    15 to 50  female    70        68        97.142857
##   1    15 to 50    male    72        32        44.444444
##   1    Over 50   female    13        13       100.000000
##   1    Over 50     male    26         5        19.230769
#

我的代碼:

 library(dplyr)
 titanic_4 <- titanic %>% 
  select(Survived, Pclass, Age, Sex) %>%
  filter(!is.na(Age)) %>%
  mutate(agecat = cut(Age, breaks = c(0, 14.99, 50, 150), 
                      include.lowest = TRUE,
                      labels = c("Under 15", "15 to 50",
                                 "Over 50"))) %>%
  group_by(Pclass,agecat,Sex) %>%
  summarize(N=n(), survivors = sum(Survived))%>%
  mutate(perc_survived = (signif((100*survivors/N), digits=8)))

print(titanic_4)

得到:

# A tibble: 18 x 6
# Groups:   Pclass, agecat [9]
   Pclass   agecat    Sex     N survivors perc_survived
    <int>   <fctr>  <chr> <int>     <int>         <dbl>
 1      1 Under 15 female     2         1     50.000000
 2      1 Under 15   male     3         3    100.000000
 3      1 15 to 50 female    70        68     97.142857
 4      1 15 to 50   male    72        32     44.444444
 5      1  Over 50 female    13        13    100.000000
 6      1  Over 50   male    26         5     19.230769
 7      2 Under 15 female    10        10    100.000000
 8      2 Under 15   male     9         9    100.000000
 9      2 15 to 50 female    61        56     91.803279
10      2 15 to 50   male    78         5      6.410256
11      2  Over 50 female     3         2     66.666667
12      2  Over 50   male    12         1      8.333333
13      3 Under 15 female    27        13     48.148148
14      3 Under 15   male    27         9     33.333333
15      3 15 to 50 female    74        33     44.594595
16      3 15 to 50   male   217        29     13.364055
17      3  Over 50 female     1         1    100.000000
18      3  Over 50   male     9         0      0.000000

當我輸入(titanic_4)時,r將最后一列中的數據四舍五入(perc_survivied):

# A tibble: 6 x 6
# Groups:   Pclass, agecat [3]
  Pclass   agecat    Sex     N survivors perc_survived
   <int>   <fctr>  <chr> <int>     <int>         <dbl>
1      1 Under 15 female     2         1      50.00000
2      1 Under 15   male     3         3     100.00000
3      1 15 to 50 female    70        68      97.14286
4      1 15 to 50   male    72        32      44.44444
5      1  Over 50 female    13        13     100.00000
6      1  Over 50   male    26         5      19.23077

但是,我希望R在perc_survived中給我六個小數位,以便它看起來像這樣:

## Pclass   agecat    Sex      N     survivors   perc_survived
## <int>   <fctr>    <chr>   <int>     <int>         <dbl>
##   1    Under 15  female     2         1        50.000000
##   1    Under 15    male     3         3       100.000000
##   1    15 to 50  female    70        68        97.142857
##   1    15 to 50    male    72        32        44.444444
##   1    Over 50   female    13        13       100.000000
##   1    Over 50     male    26         5        19.230769

誰能告訴我如何告訴r保留小數點后六位? 非常感謝!

來自評論:
*也許打印(titanic [1:6,])? –弗洛里安

我嘗試了Florian提出的方法,但並沒有改變舍入結果

> titanic_4 <- titanic %>% 
+     select(Survived, Pclass, Age, Sex) %>%
+     filter(!is.na(Age)) %>%
+     mutate(agecat = cut(Age, breaks = c(0, 14.99, 50, 150), 
+                         include.lowest = TRUE,
+                         labels = c("Under 15", "15 to 50",
+                                    "Over 50"))) %>%
+     group_by(Pclass,agecat,Sex) %>%
+     summarize(N=n(), survivors = sum(Survived))%>%
+     mutate(perc_survived = (signif((100*survivors/N), digits=8)))
> 
> print(titanic_4[1:6,])
# A tibble: 6 x 6
# Groups:   Pclass, agecat [3]
  Pclass   agecat    Sex     N survivors perc_survived
   <int>   <fctr>  <chr> <int>     <int>         <dbl>
1      1 Under 15 female     2         1      50.00000
2      1 Under 15   male     3         3     100.00000
3      1 15 to 50 female    70        68      97.14286
4      1 15 to 50   male    72        32      44.44444
5      1  Over 50 female    13        13     100.00000
6      1  Over 50   male    26         5      19.23077
> 

關於“ Eric Fail的答案”,sprintf導致列更改為字符。 這是Swirl()的賦值,swirw將不接受類型更改。

> titanic_4 <- titanic %>% 
+     select(Survived, Pclass, Age, Sex) %>%
+     filter(!is.na(Age)) %>%
+     mutate(agecat = cut(Age, breaks = c(0, 14.99, 50, 150), 
+                         include.lowest = TRUE,
+                         labels = c("Under 15", "15 to 50",
+                                    "Over 50"))) %>%
+     group_by(Pclass,agecat,Sex) %>%
+     summarize(N=n(), survivors = sum(Survived))%>%
+     mutate(perc_survived = sprintf("%0.6f",(signif((100*survivors/N), digits=8))))
> 
> head (titanic_4)
# A tibble: 6 x 6
# Groups:   Pclass, agecat [3]
  Pclass   agecat    Sex     N survivors perc_survived
   <int>   <fctr>  <chr> <int>     <int>         <chr>
1      1 Under 15 female     2         1     50.000000
2      1 Under 15   male     3         3    100.000000
3      1 15 to 50 female    70        68     97.142857
4      1 15 to 50   male    72        32     44.444444
5      1  Over 50 female    13        13    100.000000
6      1  Over 50   male    26         5     19.230769

建議使用選項(數字= 8)成功。 為了使該建議生效,在運行我的代碼之前,我必須更改r的基本選項,以便將其舍入到正確的位數。 我的r設置為四舍五入。

> options(digits=8)
> titanic_4 <- titanic %>% 
+     select(Survived, Pclass, Age, Sex) %>%
+     filter(!is.na(Age)) %>%
+     mutate(agecat = cut(Age, breaks = c(0, 14.99, 50, 150), 
+                         include.lowest = TRUE,
+                         labels = c("Under 15", "15 to 50",
+                                    "Over 50"))) %>%
+     group_by(Pclass,agecat,Sex) %>%
+     summarize(N=n(), survivors = sum(Survived))%>%
+     mutate(perc_survived = (round((100*survivors/N),digits=6)))
> 
> head (titanic_4)
# A tibble: 6 x 6
# Groups:   Pclass, agecat [3]
  Pclass   agecat    Sex     N survivors perc_survived
   <int>   <fctr>  <chr> <int>     <int>         <dbl>
1      1 Under 15 female     2         1     50.000000
2      1 Under 15   male     3         3    100.000000
3      1 15 to 50 female    70        68     97.142857
4      1 15 to 50   male    72        32     44.444444
5      1  Over 50 female    13        13    100.000000
6      1  Over 50   male    26         5     19.230769   

非常感謝您的評論和回答。 最好的祝願,

德魯

sprintf(c(.8693683839, .7869698963), fmt='%#.6g')
#> [1] "0.869368" "0.786970"

特別是針對您的情況

titanic_4 <- tibble(perc_survived = c(50.000000, 100.000000, 97.142857,
                                      44.444444, 100.000000, 19.230769))
titanic_4
#> # A tibble: 6 x 1
#>   perc_survived
#>           <dbl>
#> 1      50.00000
#> 2     100.00000
#> 3      97.14286
#> 4      44.44444
#> 5     100.00000
#> 6      19.23077
#> > 
> 
titanic_4 <- titanic_4 %>% mutate(perc_survived_6 = sprintf("%0.6f", perc_survived))
titanic_4
#> # A tibble: 6 x 2
#>   perc_survived perc_survived_6
#>           <dbl>           <chr>
#> 1      50.00000       50.000000
#> 2     100.00000      100.000000
#> 3      97.14286       97.142857
#> 4      44.44444       44.444444
#> 5     100.00000      100.000000
#> 6      19.23077       19.230769

或可能更改全局digits

options(digits=8)
titanic_4
#> # A tibble: 6 x 1
#>   perc_survived
#>           <dbl>
#> 1     50.000000
#> 2    100.000000
#> 3     97.142857
#> 4     44.444444
#> 5    100.000000
#> 6     19.230769

此問題的答案由Eric Fail在此頁面上給出。

使用options(digits = 8)更改控制台的Global Digits選項解決了head()中的舍入問題。 有關全局選項的列表,請訪問此網站。 https://stat.ethz.ch/R-manual/R-devel/library/base/html/options.html

我還創建了另一篇文章,展示了我的控制台如何取整。 G5W向我展示了如何通過調整全局數字來解決此問題。
如何告訴R正確舍入?

默認情況下,我的r studio中的全局數字設置得太低,無法正確使用round()或signf()。 我需要將數字設置為8以實現正確的舍入。

謝謝Eric Fail和G5W

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM