【问题标题】:data.frame: conditionally adding rowsdata.frame:有条件地添加行
【发布时间】:2018-12-05 22:59:47
【问题描述】:

我有一个数据框punkt_tabelle,其中包含游戏中的得分。每场比赛有 2 或 3 组(MRE 中的 3 组)。数据框包含点的制作方式。我还有一组末尾的分数,存储在scores 中。 我计算每组中每支球队的总和。 (我在total_pts 中做了这个)。

我想要实现的是将数据表(每队和每组)中的点总和与该团队根据scores 获得的点进行比较。如果这个集合中的scores 大于计算为total 的总和,那么我想在数据表中添加一个额外的行。此新行应包含团队名称,此新行的集合和技能应为 "Opp. Other Errors"Pkt 的值将是 scores 和 @987654331 之间的差异@。可能(并且在 MRE 中就是这种情况),必须为每个团队和每个集合添加一个新行。

如果您在添加新行之后重新运行 total_pts 计算,那么它将等于 scores 中的结果。

我根据这些问题和文章(R Conditional evaluation when using the pipe operator %>%Inserting a new row to data frame for each group id)尝试了以下代码的变体,但无法为我的问题找到解决方案。

这是我的代码的最后一个版本:

library(dplyr)
library (devtools)

punkt_tabelle <- structure(list(Team = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
                 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
                 1L), .Label = c("Miller/Myer", "Winter/Summer"), class = "factor"), 
                 Skill = structure(c(1L, 1L, 3L, 2L, 2L, 2L, 1L, 1L, 3L, 2L, 
                 2L, 2L, 4L, 4L, 5L, 6L, 6L, 6L, 4L, 4L, 5L, 6L, 6L, 6L), .Label = c("Attack", 
                 "Service", "Block", "Opp. Attack Error", "Opp. Block Error", 
                 "Opp. Serve Error"), class = "factor"), Set = c(2L, 3L, 2L, 
                 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 2L, 3L, 2L, 1L, 2L, 3L, 
                 1L, 2L, 3L, 1L, 2L, 3L), Pkt = c(2L, 1L, 1L, 0L, 0L, 0L, 
                 3L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 
                 0L, 1L, 0L)), row.names = c(NA, -24L), vars = c("Team", "Skill"
                 ), indices = list(0:1, 2L, 18:19, 20L, 21:23, 3:5, 6:7, 8L, 12:13, 
                 14L, 15:17, 9:11), group_sizes = c(2L, 1L, 2L, 1L, 3L, 3L, 
                 2L, 1L, 2L, 1L, 3L, 3L), biggest_group_size = 3L, labels = structure(list(
                 Team = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
                 2L, 2L), .Label = c("Miller/Myer", "Winter/Summer"), class = "factor"), 
                 Skill = c("Attack", "Block", "Opp. Attack Error", "Opp. Block Error", 
                 "Opp. Serve Error", "Service", "Attack", "Block", "Opp. Attack Error", 
                 "Opp. Block Error", "Opp. Serve Error", "Service")), row.names = c(NA, 
                 -12L), class = "data.frame", vars = c("Team", "Skill")), class = c("grouped_df", 
                  "tbl_df", "tbl", "data.frame"))



score_miller_myer <- c(3,6,3) #total points in sets 1, 2, 3
score_winter_summer <- c(5,4,5)
scores <- c(score_miller_myer, score_winter_summer)

#calculate the sum of the points per team and per set
total_pts <- punkt_tabelle %>% group_by(Team, Set) %>% summarize(total = sum(Pkt))
total_pts

#try to compare with the score and add en entry in the dataframe
punkt_tabelle %>% 
  group_by (Team, Set) %>% 
  mutate(total = sum(Pkt)) %>% 
  {if (total<scores) dplyr::bind_rows(Team=Team, Set=Set, Skill="Opp. Other Error", Pkt=(total-scores))}

punkt_tabelle

能以某种方式做到这一点吗?还是我需要使用循环并为每个组和团队手动执行此操作? 请帮忙!

编辑: 此示例中的预期输出如下所示:

Team          Skill               Set   Pkt
<fct>         <fct>             <int> <int>
 1 Miller/Myer   Attack                2     2
 2 Miller/Myer   Attack                3     1
 3 Miller/Myer   Block                 2     1
 4 Miller/Myer   Service               1     0
 5 Miller/Myer   Service               2     0
 6 Miller/Myer   Service               3     0
 7 Winter/Summer Attack                1     3
 8 Winter/Summer Attack                2     1
 9 Winter/Summer Block                 3     1
10 Winter/Summer Service               1     0
11 Winter/Summer Service               2     1
12 Winter/Summer Service               3     1
13 Winter/Summer Opp. Attack Error     2     0
14 Winter/Summer Opp. Attack Error     3     0
15 Winter/Summer Opp. Block Error      2     0
16 Winter/Summer Opp. Serve Error      1     0
17 Winter/Summer Opp. Serve Error      2     1
18 Winter/Summer Opp. Serve Error      3     1
19 Miller/Myer   Opp. Attack Error     1     1
20 Miller/Myer   Opp. Attack Error     2     0
21 Miller/Myer   Opp. Block Error      3     0
22 Miller/Myer   Opp. Serve Error      1     0
23 Miller/Myer   Opp. Serve Error      2     1
24 Miller/Myer   Opp. Serve Error      3     0
25 Winter/Summer Opp. Other Error      1     2  #here start the added rows
26 Winter/Summer Opp. Other Error      2     1  
27 Winter/Summer Opp. Other Error      3     2
28 Miller/Myer   Opp. Other Error      1     2
29 Miller/Myer   Opp. Other Error      2     2
30 Miller/Myer   Opp. Other Error      3     2

问题进一步说明: 一个团队以各种方式得分。他们要么自己得分(进攻、发球、拦网),要么他们的对手犯了错误(Opp. Attack Error、Opp. Serve Error、Opp. Block Error)。尽管如此,他们所达到的总分还是有一些差异,因为对手有一些没有具体说明的错误。为此,我想在计算差异后添加一行“Opp。其他错误”。

示例:在第 26 行:Pkt 的值为 1,因为在 Set 2 中的 total_pts 中,冬季/夏季团队有 3 分。但是根据score_winter_summerin set 2 他们的得分是 4 分。因此,在新行中添加了 1 个点的差异。

【问题讨论】:

  • 我不确定我是否理解您要执行的操作。您能否编辑您的帖子并包含您提供的示例数据的预期输出?
  • @MauritsEvers:我编辑了问题,希望现在更清楚,我还添加了预期的输出。

标签: r dataframe


【解决方案1】:

这是一种可能性。

  1. 首先,我们需要将scores 存储在data.frame 中,其中包含有关TeamSet 的信息

    df.scores <- data.frame(
        Team = c(rep("Miller/Myer", 3), rep("Winter/Summer", 3)),
        Set = 1:3,
        scores = scores)
    

    让我们检查df.scores

    df.scores
    #           Team Set scores
    #1   Miller/Myer   1      3
    #2   Miller/Myer   2      6
    #3   Miller/Myer   3      3
    #4 Winter/Summer   1      5
    #5 Winter/Summer   2      4
    #6 Winter/Summer   3      5
    
  2. 接下来,我们通过TeamSetpunk_tabelledf.scores进行左连接,通过TeamSet计算Total = sum(Pkt)的总分;然后Opp. Other ErrorscoresTotal 之间的差异给出。最终的预期输出是通过进行长到宽到长的转换来实现的。

    punkt_tabelle %>%
        left_join(df.scores) %>%
        group_by(Team, Set) %>%
        mutate(
            Total = sum(Pkt),
            `Opp. Other Error` = scores - Total) %>%
        spread(Skill, Pkt) %>%
        select(-scores, -Total) %>%
        gather(Skill, Pkt, -Team, -Set)
    #Joining, by = c("Team", "Set")
    ## A tibble: 42 x 4
    ## Groups:   Team, Set [6]
    #   Team            Set Skill              Pkt
    #   <fct>         <int> <chr>            <dbl>
    # 1 Miller/Myer       1 Opp. Other Error     2
    # 2 Miller/Myer       2 Opp. Other Error     2
    # 3 Miller/Myer       3 Opp. Other Error     2
    # 4 Winter/Summer     1 Opp. Other Error     2
    # 5 Winter/Summer     2 Opp. Other Error     1
    # 6 Winter/Summer     3 Opp. Other Error     2
    # 7 Miller/Myer       1 Attack              NA
    # 8 Miller/Myer       2 Attack               2
    # 9 Miller/Myer       3 Attack               1
    #10 Winter/Summer     1 Attack               3
    ## ... with 32 more rows
    

【讨论】:

  • 谢谢!很好 - 避免这种情况。为了完整性和未来可能的读者:library (tidyr) 需要包含在代码中。
猜你喜欢
  • 2016-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-03
  • 1970-01-01
  • 2017-03-25
相关资源
最近更新 更多