如何在pivot_wider中指定特定的列顺序

Sow*_*nan 10 r dplyr

数据帧由指示传输模式的indrur变量组成。S447.1我使用dplyr按变量分组并将其转换为宽格式。

library(dplyr)      
    indrurmodes<-indrur %>% 
          group_by(V024,V025,V190) %>%count(S447.1) %>% 
          pivot_wider(names_from = S447.1,values_from = n, values_fill = list(n = 0))
Run Code Online (Sandbox Code Playgroud)

它给了我以下输出。但我想要按特定顺序排列的列 - GovtAmb,OthAmb,Jeep,Tempo,Scooter,Bus,Cart,Foot,NA。我必须对多个过滤后的数据帧执行上述代码。每次,我都会收到不同的订单。这使得在我使用列索引作为参考时执行进一步的算术运算变得困难。如何保持所需的列顺序?

       V024     V025    V190 GovtAmb Jeep   Tempo Foot  NA     OthAmb Scooter  Bus  Cart
3   Andaman     Rural   Middle  67   21     16     16   615     2     3        4      0
4   Andaman     Rural   Richer  66   39     20     3    617     2     1        0      0
5   Andaman     Rural   Richest 21   18     5      2    278     0     2        5      1
6   Andhra      Rural   Poorest 25   0      35     4    294     5     0        6      0
Run Code Online (Sandbox Code Playgroud)

Ron*_*hah 17

你有两个选择。

library(dplyr)
library(tidyr)

#Order of the columns you want
vec <- c("GovtAmb","OthAmb","Jeep","Tempo","Scooter","Bus","Cart","Foot",NA)
Run Code Online (Sandbox Code Playgroud)

1)使用pivot_wider

pivot_wider按列出现的顺序返回列。所以你可以arrange在使用之前按照你想要的顺序排列数据pivot_wider

indrur %>% 
   group_by(V024,V025,V190) %>%
   count(S447.1) %>% 
   arrange(match(S447.1, vec)) %>%
   pivot_wider(names_from = S447.1,values_from = n, values_fill = list(n = 0))
Run Code Online (Sandbox Code Playgroud)

2)使用旧的spread,它根据因子水平给出列顺序。

indrur %>% 
  group_by(V024,V025,V190) %>%
  count(S447.1) %>% 
  mutate(S447.1 = factor(S447.1, levels = vec)) %>%
  spread(S447.1,n,fill = 0)
Run Code Online (Sandbox Code Playgroud)

  • 在我看来,`spread` 的行为更好。我希望pivot_wider能像那样工作...... (3认同)