简体   繁体   English

如何将每隔一行移动到 R 中的新列

[英]How to move every other row to a new column in R

I have a data frame that is supposed to show the winners of a tournament and their opponents.我有一个数据框,应该显示锦标赛的获胜者和他们的对手。 Currently the loser is in every other row.目前,输家在每隔一排。 So, row 1 is the winner, row 2 is the loser, row 3 is the winner, row 4 is the loser, and so on.所以,第 1 行是赢家,第 2 行是输家,第 3 行是赢家,第 4 行是输家,依此类推。

I want the winner and their opponent to be next to each other so that it's easier to see who competed against who.我希望获胜者和他们的对手彼此相邻,以便更容易看到谁与谁竞争。 The tricky part is keeping the gym, names, and competitor number for each person together in the same row.棘手的部分是将每个人的健身房、姓名和参赛者编号保持在同一行中。

How do I move every other row to a new column so that the winner and their opponent are in the same row?如何将每隔一行移动到一个新列,以便获胜者和他们的对手在同一行?

y = read.csv('https://raw.githubusercontent.com/bandcar/Examples/main/y.csv')

# FAILED ATTEMPT
library(data.table)
z=dcast(setDT(y)[, grp := gl(.N, 2, .N)], grp ~ rowid(grp),
        value.var = setdiff(names(y), 'grp'))[, grp := NULL][]

Note that both photos are different data sets请注意,两张照片是不同的数据集

What my df currently looks like:我的 df 目前的样子:

我的 df 目前的样子

Similar to what I want it to look like:类似于我想要的样子: 我希望它看起来像什么

Using dplyr you could do:使用dplyr你可以这样做:

library(dplyr)

read.csv('https://raw.githubusercontent.com/bandcar/Examples/main/y.csv') %>%
  group_by(fight, date) %>%
  summarise(division = first(division),
            competitor_1 = first(competitor),
            name_1 = first(name),
            competitor_2 = last(competitor),
            name_2 = last(name))
#> `summarise()` has grouped output by 'fight'. You can override using the
#> `.groups` argument.
#> # A tibble: 61 x 7
#> # Groups:   fight [26]
#>    fight    date                  division competitor_1 name_1    compe~1 name_2
#>    <chr>    <chr>                 <chr>           <int> <chr>       <int> <chr> 
#>  1 BYE      BYE                   Master 2            1 Rafael M~       1 Rafae~
#>  2 FIGHT 19 Thu 09/01 at 12:14 PM Master 2            2 Piter Fr~      63 Alan ~
#>  3 FIGHT 20 Thu 09/01 at 01:01 PM Master 2           16 Marques ~      55 Diego~
#>  4 FIGHT 20 Thu 09/01 at 12:13 PM Master 2           28 Kenned D~      44 Verge~
#>  5 FIGHT 22 Thu 09/01 at 12:27 PM Master 2            4 Marcus V~      52 Kian ~
#>  6 FIGHT 23 Thu 09/01 at 12:33 PM Master 2           30 Adam Col~      46 Steph~
#>  7 FIGHT 23 Thu 09/01 at 12:54 PM Master 2           31 Namrod B~      47 Stefa~
#>  8 FIGHT 23 Thu 09/01 at 12:58 PM Master 2           13 David Ch~      53 Joshu~
#>  9 FIGHT 24 Thu 09/01 at 01:08 PM Master 2            3 Sandro G~      56 Carlo~
#> 10 FIGHT 24 Thu 09/01 at 12:35 PM Master 2            8 Rafael R~      60 Andre~
#> # ... with 51 more rows, and abbreviated variable name 1: competitor_2

Created on 2022-09-16 with reprex v2.0.2使用reprex v2.0.2创建于 2022-09-16

There are some problems with your dataset, eg for "FIGHT 22" there are four entries (from your description I expected two entries).您的数据集存在一些问题,例如“FIGHT 22”有四个条目(根据您的描述,我预计有两个条目)。

  division gender belt  weight fight    date                  competitor name                   gym                 
  <chr>    <chr>  <chr> <chr>  <chr>    <chr>                      <dbl> <chr>                  <chr>               
1 Master 2 Male   BLACK Middle FIGHT 22 Thu 09/01 at 12:27 PM          4 Marcus V. C. Antelante Ares BJJ            
2 Master 2 Male   BLACK Middle FIGHT 22 Thu 09/01 at 12:27 PM         62 Andrew E. Ganthier     Renzo Gracie Academy
3 Master 2 Male   BLACK Middle FIGHT 22 Thu 09/01 at 12:27 PM         11 Jimmy Dang Khoa Tat    CheckMat            
4 Master 2 Male   BLACK Middle FIGHT 22 Thu 09/01 at 12:27 PM         52 Kian Takumi Kadota     Brasa CTA           

The same problem exists for fights 26 and 35. Assuming these are corrected, and assuming odd rows contain winners and even rows contain losers, the following code should work (using tidyverse):第 26 场和第 35 场比赛也存在同样的问题。假设这些问题已得到纠正,并假设奇数行包含赢家,偶数行包含输家,以下代码应该可以工作(使用 tidyverse):

y %>% 
    mutate(outcome = if_else(row_number() %% 2 == 1, "winner", "loser")) %>%     
    pivot_wider(names_from = outcome, values_from = c(competitor, name, gym)) 

This will get you close to what you want.这会让你接近你想要的。 It adds the winner column.它添加了获胜者列。 Odd number index is winner and even number index is loser.奇数指数为赢家,偶数指数为输家。 I removed the BYE week rows for aesthetics.为了美观,我删除了 BYE 周行。 Then we group by the date and fight and keep the desired data from the combined rows and expand the summarised columns to the winner loser information.然后我们按日期分组并战斗并保留组合行中的所需数据,并将汇总列扩展为获胜者失败者信息。

library(dplyr)

y %>%
    mutate(
        winner = ifelse((y$X %% 2) == 0,'loser','winner')) %>%
    filter(date != 'BYE') %>%
    group_by(date, fight) %>% 
    summarise(division = first(division),
              belt = first(belt),
              weight = first(weight),
              gender = first(gender),
              winner.rank = first(competitor),
              winner = first(name),
              winner.gym = first(gym),
              opp.rank= last(competitor),
              opponent = last(name),
              opponent.gym = last(gym))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM