簡體   English   中英

拆分,重塑,使用單管道中的tidyverse綁定堆疊的寬數據

[英]Split, reshape, bind stacked wide data using tidyverse in single pipe

我有一個單一的數據框,它為多個地區和村庄堆積了大量數據,其中有政黨記錄的投票。 每個地區都有不同的政黨:

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()

這是正確的嗎?

我們也可以這樣做

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
 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()

更短,並具有適當的列名稱。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM