Search code examples
rloopscomparisonrow

R Comparing row values in one column given that 3 conditions are fulfilled


Although I have searched a lot on stackoverflow I could not find an answer for my problem. It is quite tricky because I want only to compare row values in one column given that 3 conditions are fulfilled. I want to compare 2 row values within the same column (Result) if and only if they they are in the same treatment, group and period. My goal is then to evaluate whether player 1 and player 2 scored the same result (1 = if yes, 0= if no). In total, I have five columns: Treatment; Player; Group; Result; Period. Below the table.

    df
T   Player    Group      Result  Period    Same Result
1     1         6          20      1          1
1     2         6          20      1          1
1     1         5          20      1          0
1     2         1          20      1          1
1     1         1          20      1          1
1     2         2          20      1          1
1     1         2          20      1          1
1     2         4          120     1          1
1     1         3          20      1          1
1     2         3          20      1          1
1     1         4          120     1          1
1     2         5          120     1          0

2     1         2          20      1          1
2     2         1          120     1          1
2     1         4          20      1          0
2     2         5          20      1          1
2     1         6          20      1          1
2     2         2          20      1          1
2     1         3          20      1          1 
2     2         3          20      1          1
2     1         1          120     1          1
2     2         6          20      1          1
2     1         5          20      1          1
2     2         4          120     1          0

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


Solution

  • With tidyverse you can do the following. First, group_by your 3 columns of interest: treatment, group, and period. Then, use n_distinct for each group to see if only 1 unique result for that group.

    library(tidyverse)
    
    df %>%
      group_by(T, Group, Period) %>%
      mutate(Same_Result_2 = +(n_distinct(Result) == 1)) 
    

    Alternative with data.table package:

    library(data.table)
    
    setDT(df)
    df[, Same_Result_3 := if(uniqueN(Result)==1) 1 else 0, by=.(T, Group, Period)]
    

    Output

    Same_Result_1 is value provided in example. Same_Result_2 is determined by above tidyverse code.

    # A tibble: 24 x 7
    # Groups:   T, Group, Period [12]
           T Player Group Result Period Same_Result_1 Same_Result_2
       <int>  <int> <int>  <int>  <int>         <int>         <int>
     1     1      1     6     20      1             1             1
     2     1      2     6     20      1             1             1
     3     1      1     5     20      1             0             0
     4     1      2     1     20      1             1             1
     5     1      1     1     20      1             1             1
     6     1      2     2     20      1             1             1
     7     1      1     2     20      1             1             1
     8     1      2     4    120      1             1             1
     9     1      1     3     20      1             1             1
    10     1      2     3     20      1             1             1
    11     1      1     4    120      1             1             1
    12     1      2     5    120      1             0             0
    13     2      1     2     20      1             1             1
    14     2      2     1    120      1             1             1
    15     2      1     4     20      1             0             0
    16     2      2     5     20      1             1             1
    17     2      1     6     20      1             1             1
    18     2      2     2     20      1             1             1
    19     2      1     3     20      1             1             1
    20     2      2     3     20      1             1             1
    21     2      1     1    120      1             1             1
    22     2      2     6     20      1             1             1
    23     2      1     5     20      1             1             1
    24     2      2     4    120      1             0             0