r - 控制台中head()的列中的文本对齐方式

时间:2018-01-21 18:08:38

标签: r center text-alignment

r - 当我在控制台中输入()数据时,如何让表格的每一列中的文字与中心对齐?

我的目标是让我的桌子看起来像这样:

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

目前,我的表格如下:

> 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

这是我的代码:

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 = survivors/N*100)

head (titanic_4)

0 个答案:

没有答案
相关问题