DPLYR变异功能转换为"转换"?

时间:2017-10-14 00:52:04

标签: r dplyr mutate

几周前我才刚开始接R。我试图转换(如果这是正确的R术语?)性别行到列时遇到一些问题。

    > brfss %>%
    +     filter(hours1 >=1,hours1 <=24) %>%
    +     group_by(hours1, gender) %>%
    +     summarise(count = n())
    # A tibble: 48 x 3
    # Groups:   hours1 [?]
       hours1   gender count
          <int> <fctr> <int>
     1        1   Male    96
     2        1 Female   132
     3        2   Male   464
     4        2 Female   612
     5        3   Male  1433
     6        3 Female  2063
     7        4   Male  5749
     8        4 Female  8512
     9        5   Male 13231
    10        5 Female 20205

    # ... with 38 more rows

我想将列显示为:

    hours1   Male  Female count

基本上将整个男性/女性转化为列而不是行。有人可以给我一些指示吗?感谢。

2 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

library(dplyr)
library(tidyr)
library(magrittr)

brfss2013 %>% 
  select(hours1 = sleptim1, gender = sex) %>% 
  na.omit() %>%
  group_by(hours1, gender) %>%
  summarize(count = n()) %>%
  spread(key = gender, value = count)

答案 1 :(得分:-4)

试试这个.. brfss%&gt;%   传播(关键=性别,价值=性别)%&gt;%   选择(小时1,男,女,计数)