我正在处理来自 r Programming Environment 12 Data Manipulation 的一个问题。我不知道如何让 r 在小数点后给我正确的位数。
我的代码:
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 = (survivors/N)*100.000000)
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.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 保留小数点后 6 位?
我试过 sprintf:
> 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("%.6f",((survivors/N)*100.000000)))
>
> 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
添加 sprintf 可以纠正小数位的问题,但它产生了一个新问题。sprintf 将列类型从 更改<dbl>
为<chr>
。
漩涡不会接受这个答案。有谁知道另一种方式?
太感谢了!