收集多个列

Gathering multiple columns

我正在尝试在 R 中收集多个列。一个例子是一些最近的梦幻足球数据:

homeId homePlayerName homePosition homeRosterPts awayId awayPlayerName awayPosition awayRosterPts
5      Nick Chubb           RB         23.3         1   Josh Jacobs         RB           8.9
4      Tyreek Hill          WR         24.6         10   Patrick Mahomes    QB           18.5
7      Davante Adams        WR         21.0         2   Michael Thomas      WR           12.4

我希望 ID、球员姓名、积分和位置列各有一个列,这样我就可以整理数据并将行绑定在一起 - 而不是在主场和客场重复。我需要在这里使用收集功能吗?任何解决方案都会有所帮助!

gather 已停用,请使用 pivot_longer 以简化操作:

tidyr::pivot_longer(df, 
                    cols = everything(), 
                    names_to = c('game', '.value'),
                    names_pattern = '(home|away)(.*)')

#  game     Id PlayerName     Position RosterPts
#  <chr> <int> <chr>          <chr>        <dbl>
#1 home      5 NickChubb      RB            23.3
#2 away      1 JoshJacobs     RB             8.9
#3 home      4 TyreekHill     WR            24.6
#4 away     10 PatrickMahomes QB            18.5
#5 home      7 DavanteAdams   WR            21  
#6 away      2 MichaelThomas  WR            12.4

数据

df <- structure(list(homeId = c(5L, 4L, 7L), homePlayerName = c("NickChubb", 
"TyreekHill", "DavanteAdams"), homePosition = c("RB", "WR", "WR"
), homeRosterPts = c(23.3, 24.6, 21), awayId = c(1L, 10L, 2L), 
    awayPlayerName = c("JoshJacobs", "PatrickMahomes", "MichaelThomas"
    ), awayPosition = c("RB", "QB", "WR"), awayRosterPts = c(8.9, 
    18.5, 12.4)), class = "data.frame", row.names = c(NA, -3L))

对于 fun/pain/education,以下是使用基础 R 中的 reshape 的方法:

reshape(setNames(df, gsub("(home|away)(.*)", "\2.\1", names(df))), 
        direction = "long", varying = 1:ncol(df))
#        time Id     PlayerName Position RosterPts id
# 1.home home  5      NickChubb       RB      23.3  1
# 2.home home  4     TyreekHill       WR      24.6  2
# 3.home home  7   DavanteAdams       WR      21.0  3
# 1.away away  1     JoshJacobs       RB       8.9  1
# 2.away away 10 PatrickMahomes       QB      18.5  2
# 3.away away  2  MichaelThomas       WR      12.4  3

您也可以像这样使用“data.table”中的 melt

library(data.table)
nam <- unique(sub("home|away", "", names(df)))
melt(as.data.table(df), measure = patterns(nam), value.name = nam)
#    variable Id     PlayerName Position RosterPts
# 1:        1  5      NickChubb       RB      23.3
# 2:        1  4     TyreekHill       WR      24.6
# 3:        1  7   DavanteAdams       WR      21.0
# 4:        2  1     JoshJacobs       RB       8.9
# 5:        2 10 PatrickMahomes       QB      18.5
# 6:        2  2  MichaelThomas       WR      12.4