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.
My code:
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)
Gives:
# 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
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
Can anyone tell me how to tell r to keep 6 decimal place?
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)
Which gives:
# 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
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 will not accept this answer. Does anyone know another way?
Thank you so much!