如何融合或收集基于多列的数据框?

时间:2019-12-17 17:54:03

标签: r reshape melt

以下是我拥有的数据框的类型和格式的示例

fnum<-c('510-1','510-2','510-3')
refcharge<-c('assault','theft','rape')
refsev<-c('F','F','F')
refclass<-c('B','B','A')
issuedch<-c('assault','robbery','sodomy')
isssev<-c('F','M','F')
issclass<-c('A','A','B')

df<-data.frame(fnum,refcharge,refsev,refclass,issuedch,isssev,issclass)
df

     fnum refcharge refsev refclass issuedch isssev issclass
1 510-1   assault      F        B  assault      F        A
2 510-2     theft      F        B  robbery      M        A
3 510-3      rape      F        A   sodomy      F        B

我希望能够gathermelt将此数据框设置为更高的数据集,使其看起来像以下格式。我提供了一个示例,显示了fnum 510-2

的外观
fnum chargeoutcome charge Severity Class
510-1  refcharge   assault  F       B
510-1  issuedch    assault  F       A
510-2  refcharge   theft    F       B
510-2  issuedch    robberry M       A
510-3  refcharge   rape     F       A
510-3  issuedch    sodomy   F       B

我使用了melt(df,id=c('fnum','refcharge','issuedch')),但是它没有以我需要的数据框的格式提供

1 个答案:

答案 0 :(得分:0)

我们可以使用names_pattern中的pivot_longer

library(dplyr)
library(tidyr)
df %>%
  pivot_longer(cols = -fnum, names_pattern = "^(ref|iss)(.*)", 
       names_to = c('chargeoutcome', '.value')) %>% 
  mutate(charge = coalesce(as.character(charge), as.character(uedch))) %>% 
  select(-uedch) %>%
  mutate(chargeoutcome = factor(chargeoutcome, levels = c('ref', 'iss'), 
          labels = c('refcharge', 'issuedch')))
# A tibble: 6 x 5
#  fnum  chargeoutcome charge  sev   class
#  <fct> <fct>         <chr>   <fct> <fct>
#1 510-1 refcharge     assault F     B    
#2 510-1 issuedch      assault F     A    
#3 510-2 refcharge     theft   F     B    
#4 510-2 issuedch      robbery M     A    
#5 510-3 refcharge     rape    F     A    
#6 510-3 issuedch      sodomy  F     B