使用tidyverse在单个管道中分割,重塑,绑定堆叠的宽数据

时间:2019-03-21 04:07:24

标签: r dplyr tidyr purrr

我有一个数据框,其中堆叠了多个地区和村庄的广泛数据,那里记录了政党的选票。每个地区都有不同的政党:

df_in <- data.frame(
  X1 = c(rep("District1", 3), rep("District2", 3)),
  X2 = c(rep(c("", "Village1", "Village2"), 2)),
  X3 = c("Party1", "30", "11", "Party1", "2", "59"),
  X4 = c("Party2", "55", "42", "Party2", "66", "44"),
  X5 = c("", "", "", "Party3", "32", "13"),
  X6 = c("", "", "", "Party4", "99", "75")
)

最后,我想为每个村庄/地区的每一方记录一长串投票数据:

df_out <- data.frame(
  X1 = c(rep("District1", 4), rep("District2", 8)),
  X2 = c("Village1", "Village1", "Village2", "Village2", "Village1", "Village1", "Village1", "Village1", "Village2", "Village2", "Village2", "Village2"),
  X3 = c(
    rep(c("Party1", "Party2"), 2), 
    rep(c("Party1", "Party2", "Party3", "Party4"), 2)
    ),
  X4 = c(30, 55, 11, 42, 2, 66, 32, 99, 59, 44, 13, 75)
)

我想在单个管道中从输入到输出。我一直在努力进行以下设置,但到目前为止没有成功:

df_out <- df_in %>% 
  split(.$X1) %>% 
  map() %>% 
  gather() %>% 
  bind_rows()

这是对的吗?

3 个答案:

答案 0 :(得分:2)

我们也可以使用

library(dplyr)
library(tidyr)
library(hablar)
df_in %>% 
   # group by 'X1'
   group_by(X1) %>%
   # remove the first row
   slice(-1) %>% 
   # ungroup
   ungroup %>%
   # rename the column names with 'Party'
   rename_at(vars(X3:X6), ~ paste0("Party", 1:4)) %>%   
   # change the type of columns    
   retype %>%
   # gather into long format
   gather(X3, X4, Party1:Party4, na.rm = TRUE) %>%
   # arrange if needed
   arrange(X1, X2)
# A tibble: 12 x 4
#   X1        X2       X3        X4
#   <chr>     <chr>    <chr>  <int>
# 1 District1 Village1 Party1    30
# 2 District1 Village1 Party2    55
# 3 District1 Village2 Party1    11
# 4 District1 Village2 Party2    42
# 5 District2 Village1 Party1     2
# 6 District2 Village1 Party2    66
# 7 District2 Village1 Party3    32
# 8 District2 Village1 Party4    99
# 9 District2 Village2 Party1    59
#10 District2 Village2 Party2    44
#11 District2 Village2 Party3    13
#12 District2 Village2 Party4    75

答案 1 :(得分:1)

 library(tidyverse)
 df_in %>%   
  split(.$X1) %>% 
  map(. %>% gather(key,val,X3:X6) %>% 
        group_by(key) %>% mutate(key1=first(val)) %>% filter(row_number() %in% 2:n() & val!="") %>% 
        ungroup() %>% rename(X4=val, X3=key1) %>% select(X1,X2,X3,X4)) %>% 
 bind_rows()

答案 2 :(得分:1)

更短并且具有正确的列名。

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

df_in %>%
    mutate_all(as.character) %>%  # Or set stringsAsFactors = FALSE
    set_names(c("district", "village", paste0("Party", 1:4))) %>% 
    filter(nchar(village) > 0) %>% 
    gather(party, votes, -district, -village) %>% 
    mutate(votes = as.integer(votes) %>% replace_na(0)) %>% 
    arrange(district, village, party) %>% 
    filter(votes > 0)

##     district  village  party votes
## 1  District1 Village1 Party1    30
## 2  District1 Village1 Party2    55
## 3  District1 Village2 Party1    11
## 4  District1 Village2 Party2    42
## 5  District2 Village1 Party1     2
## 6  District2 Village1 Party2    66
## 7  District2 Village1 Party3    32
## 8  District2 Village1 Party4    99
## 9  District2 Village2 Party1    59
## 10 District2 Village2 Party2    44
## 11 District2 Village2 Party3    13
## 12 District2 Village2 Party4    75