十进制位置增加-旋流-r编程环境12-数据处理 [英] Increasing Decimal Positions - Swirl - r Programming Environment 12 - Data Manipulation
问题描述
我正在处理漩涡中的一个问题,编程环境12数据处理。我无法弄清楚如何让r给我小数点后的位数。
I am working on a question from swirl, r Programming Environment 12 Data Manipulation. I cannot figure out how to get r to give me the right number of digits after the decimal place.
我的代码:
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中给我小数点后六位,以便它看起来像这样:
However, I would like R to give me six decimal places in perc_survived so that it will look like this:
## 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位吗?
Can anyone tell me how to tell r to keep 6 decimal place?
我尝试过sprintf:
I have tried 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>
。
Adding sprintf corrects the problem with the decimal places, but it has created a new problem. sprintf changed the column type from <dbl>
to <chr>
.
Swirl将不接受此答案。有人知道另一种方式吗?
Swirl will not accept this answer. Does anyone know another way?
非常感谢!
推荐答案
sprintf
是一个字符串操作函数,因此根据定义它将返回一个字符串。如果您只是想舍入到一定位数,则舍入
或 signif
(有效数字)应该工作。两者都具有要保留的位数的参数。看来 mutate(perc_survived = round((survivors / N)* 100,digits = 6))
会为您提供所需的内容。如果您想要有效数字的数量,而不是简单的舍入,请使用 signif
。
sprintf
is a string manipulation function, so it by definition will return a string. If you're simply trying to round to a set number of digits, either round
or signif
(significant figures) should work. Both have parameters for the number of digits to keep. So it seems like mutate(perc_survived = round((survivors / N) * 100, digits = 6))
would give you what you're looking for. If you want the number of significant figures, rather than a simple rounding, use signif
.
这篇关于十进制位置增加-旋流-r编程环境12-数据处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!