Home > other >  Identify missing number in a new column
Identify missing number in a new column

Time:02-01

I have a data frame called 'dat' that has two separate columns to denote the order that participants completed a study. It looks like the following:

enter image description here

However, in total there were 3 conditions, and so I would like to add a new column called 'middle' which contains the missing value (for example, for participant 1 this missing value is 2). The rest of the data would like below:

enter image description here

Can anyone help?

CodePudding user response:

Note that the three columns first, last and middle must add up to 6 in each row, so if you subtract first and last from 6, you will get middle:

within(dat, middle <- 6 - first - last)
#>   ppt_num first last middle
#> 1       1     1    3      2
#> 2       2     2    1      3
#> 3       3     2    3      1
#> 4       4     3    2      1
#> 5       5     2    1      3
#> 6       6     1    2      3

Created on 2022-01-31 by the reprex package (v2.0.1)

  •  Tags:  
  • Related