首页 > 解决方案 > 枢轴/重塑数据框(保持订单/标识符完整) dplyr tidyverse

问题描述

我怎样才能重塑这个数据框,使每一行代表一个球员的体重和得分(r)。每个玩家都用数字标记标识(玩家“001”、“002”等)。

我有一个包含许多球员和分数的大型数据框,需要像这样重塑:

rm(list = ls())
df <- data.frame(gameround= c("1_1", "1_2", "1_3"),
  r001 = c(3,5,4), r002 = c(2,3,5), r003 = c(1,2,2), weight001=c(0.7,0.8,0.7), 
                 weight002 = c(0.6,0.1,0.6), weight003=c(0.2,0.7,0.2))

#current output
#gameround r001 r002 r003 weight001 weight002 weight003  
#1_1         3    2    1       0.7       0.6       0.2
#1_2         5    3    2       0.8       0.1       0.7
#1_3         4    5    2       0.7       0.6       0.2

#desired output (it is also okay to leave the weightid and rid out if a 
#certain method can ascertain that the weight-player pairs remain intact)

#gameround weightid  weight   rid    r
#1_1        001        0.7    001    3
#1_1        002        0.6    002    2
#1_1        003        0.2    003    1
#1_2        001        0.8    001    5
#1_2        002        0.1    002    3
#1_2        003        0.7    003    2
#1_3        001        0.7    001    4
#1_3        002        0.6    002    5
#1_3        003        0.2    003    2

标签: rpivottidyversereshapetidyr

解决方案


使用pivot_longer-

tidyr::pivot_longer(df, 
                    cols = -gameround, 
                    names_to = c('.value', 'id'), 
                    names_pattern = '([a-z]+)(\\d+)')

#  gameround id        r weight
#  <chr>     <chr> <dbl>  <dbl>
#1 1_1       001       3    0.7
#2 1_1       002       2    0.6
#3 1_1       003       1    0.2
#4 1_2       001       5    0.8
#5 1_2       002       3    0.1
#6 1_2       003       2    0.7
#7 1_3       001       4    0.7
#8 1_3       002       5    0.6
#9 1_3       003       2    0.2

推荐阅读