Search code examples
rcopycomparisontidyverserows

R Insert Value within Dataframe


I have a very complex problem, i hope someone can help -> i want to copy a row value (i.e. Player 1 or Player 2) into two other rows (for Player 3 and 4) if and only if these players are in the same Treatment, Group and Period AND this player was indeed picked (see column Player.Picked)

I know that with tidyverse I can group_by my columns of interest: Treatment, Group, and Period. However, I am unsure how to proceed with the condition that Player Picked is fulfilled and then how to extract this value appropriately for the players 3 and 4 in the same treatment, group, period.

The column "extracted.Player 1/2 Value" should be the output. (I have manually provided the first four correct solutions).

Any ideas? Help would be very much appreciated. Thanks a lot in advance!

df
T   Player    Group      Player.Picked   Period     Player1/2Value         extracted.Player1/2Value
1     1         6              1         1          10
1     2         6              1         1          9 
1     3         5              2         1          NA                          -> 4
1     4         6              1         1          NA                          -> 10
1     5         3              1         1          NA                      
1     1         5              2         1          8                           
1     2         1              0         1          7
1     3         6              1         1          NA                          -> 10
1     4         2              2         1          NA
1     5         2              2         1          NA
1     1         1              0         1          7
1     2         2              2         1          11
1     3         3              1         1          NA
1     4         4              1         1          NA   
1     5         4              1         1          NA
1     1         2              2         1          21
1     2         4              1         1          17
1     3         1              0         1          NA
1     4         5              2         1          NA                           -> 4
1     5         6              1         1          NA  
1     1         3              1         1          12
1     2         3              1         1          15
1     3         4              1         1          NA
1     4         1              0         1          NA
1     5         1              0         1          NA
1     1         4              1         1          11 
1     2         5              2         1          4
1     3         2              2         1          NA
1     4         3              1         1          NA
1     5         5              2         1          NA
 

Solution

  • I'm not sure if I understood the required logic; here I'm assuming that Player 5 always picks Player 1 or 2 per Group.

    So, here is my go at this using library(data.table):

    library(data.table)
    
    DT <- data.table::data.table(
                     check.names = FALSE,
                               T = c(1L,1L,1L,
                                     1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
                                     1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
                                     1L,1L,1L,1L),
                          Player = c(1L,2L,3L,
                                     4L,5L,1L,2L,3L,4L,5L,1L,2L,3L,4L,5L,
                                     1L,2L,3L,4L,5L,1L,2L,3L,4L,5L,1L,
                                     2L,3L,4L,5L),
                           Group = c(6L,6L,5L,
                                     6L,3L,5L,1L,6L,2L,2L,1L,2L,3L,4L,4L,
                                     2L,4L,1L,5L,6L,3L,3L,4L,1L,1L,4L,
                                     5L,2L,3L,5L),
                   Player.Picked = c(1L,1L,2L,
                                     1L,1L,2L,0L,1L,2L,2L,0L,2L,1L,1L,1L,
                                     2L,1L,0L,2L,1L,1L,1L,1L,1L,0L,0L,
                                     1L,2L,2L,2L),
                          Period = c(1L,1L,1L,
                                     1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
                                     1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
                                     1L,1L,1L,1L),
                `Player1/2Value` = c(10L,9L,NA,
                                     NA,NA,8L,7L,NA,NA,NA,7L,11L,NA,NA,
                                     NA,21L,17L,NA,NA,NA,12L,15L,NA,NA,NA,
                                     11L,4L,NA,NA,NA),
      `extracted.Player1/2Value` = c(NA,NA,4L,
                                     10L,NA,NA,NA,10L,NA,NA,NA,NA,NA,NA,
                                     NA,NA,NA,NA,4L,NA,NA,NA,NA,NA,NA,NA,
                                     NA,NA,NA,NA)
    )
    
    setorderv(DT, cols = c("T", "Group", "Period", "Player"))
    
    Player5PickedDT <- DT[Player == 5, Player.Picked, by = c("T", "Group", "Period")]
    setnames(Player5PickedDT, old = "Player.Picked", new = "Player5Picked")
    DT <- DT[Player5PickedDT, on = c("T", "Group", "Period")]
    
    extractedDT <- DT[Player == Player5Picked & Player5Picked > 0, `Player1/2Value`, by = c("T", "Group", "Period")]
    setnames(extractedDT, old = "Player1/2Value", new = "extractedValue")
    DT[, "Player5Picked" := NULL]
    
    DT <- extractedDT[DT, on = c("T", "Group", "Period")]
    DT[, extractedValue := fifelse(Player %in% c(3, 4), yes = extractedValue, no = NA_real_)]
    
    setcolorder(DT, c("T", "Group", "Period", "Player", "Player.Picked", "Player1/2Value", "extracted.Player1/2Value", "extractedValue"))
    
    DT
    

    The resulting table differs from your expected result (extracted.Player1/2Value vs extractedValue, but in my eyes it is following the explained logic):

        T Group Period Player Player.Picked Player1/2Value extracted.Player1/2Value extractedValue
     1: 1     1      1      1             0              7                       NA             NA
     2: 1     1      1      2             0              7                       NA             NA
     3: 1     1      1      3             0             NA                       NA             NA
     4: 1     1      1      4             1             NA                       NA             NA
     5: 1     1      1      5             0             NA                       NA             NA
     6: 1     2      1      1             2             21                       NA             NA
     7: 1     2      1      2             2             11                       NA             NA
     8: 1     2      1      3             2             NA                       NA             11
     9: 1     2      1      4             2             NA                       NA             11
    10: 1     2      1      5             2             NA                       NA             NA
    11: 1     3      1      1             1             12                       NA             NA
    12: 1     3      1      2             1             15                       NA             NA
    13: 1     3      1      3             1             NA                       NA             12
    14: 1     3      1      4             2             NA                       NA             12
    15: 1     3      1      5             1             NA                       NA             NA
    16: 1     4      1      1             0             11                       NA             NA
    17: 1     4      1      2             1             17                       NA             NA
    18: 1     4      1      3             1             NA                       NA             11
    19: 1     4      1      4             1             NA                       NA             11
    20: 1     4      1      5             1             NA                       NA             NA
    21: 1     5      1      1             2              8                       NA             NA
    22: 1     5      1      2             1              4                       NA             NA
    23: 1     5      1      3             2             NA                        4              4
    24: 1     5      1      4             2             NA                        4              4
    25: 1     5      1      5             2             NA                       NA             NA
    26: 1     6      1      1             1             10                       NA             NA
    27: 1     6      1      2             1              9                       NA             NA
    28: 1     6      1      3             1             NA                       10             10
    29: 1     6      1      4             1             NA                       10             10
    30: 1     6      1      5             1             NA                       NA             NA
        T Group Period Player Player.Picked Player1/2Value extracted.Player1/2Value extractedValue