Experience Sampling Data-Wrangling

Experience sampling (ESM) is a popular method used in psychological research because it better enables the testing of hypotheses in real-world environments (Conner et al. 2009). While the flexibility of R is ideal for cleaning and analyzing ESM data, it takes time to figure out the packages, functions, and code needed to achieve these ends. In the present article I provide guidance in this regard on some key ESM data-wrangling steps.

Merging datasets

Merging datasets is often essential after ESM data collection given questionnaires sent out during the ESM phase are frequently flanked by intake and outtake questionnaires (e.g., to measure age, gender, etc.). To examine relations between variables measured from these datasets all variables must be in the same dataset. Merging datasets is therefore a necessity.

So how would I go about merging the below (sample) ESM phase dataset…

PID DAY SIG NumCEpi Satisfied MoreGrati NumCBeh ActEnj ActCon ExpEnj
72320021024 1 1 3 4 5 2 4 4 2
72320021024 1 2 0 4 5 2 5 3 3
72320021024 1 3 2 5 5 2 3 6 6
72320021024 1 4 1 5 6 1 5 5 5
72320021024 1 5 1 3 6 1 3 5 5
72320021024 1 7 2 2 1 1 2 4 3
72320021024 2 1 1 5 2 2 5 5 5
72320021024 2 2 1 3 4 1 3 6 2
72320021024 2 3 1 3 6 1 3 2 2
72320021024 2 4 1 4 5 2 5 4 6
72320021024 2 5 1 4 5 1 2 4 3
72320021024 2 6 1 3 6 2 3 4 4
72320021024 2 7 1 3 5 2 4 4 3
72320021024 3 2 1 5 5 2 5 4 5
72320021024 3 3 1 4 5 2 4 5 5
72320021024 3 4 0 4 6 2 5 5 4
72320021024 3 5 1 4 5 1 4 3 3
72320021024 3 6 1 5 4 1 5 6 5
72320021024 3 7 1 5 5 2 4 5 4
72320021024 4 1 1 5 3 1 5 4 5
72320021024 4 2 1 4 5 1 3 6 5
72320021024 4 3 1 3 3 1 4 4 3
72320021024 4 4 1 4 5 1 4 3 4
72320021024 4 5 1 4 6 1 4 5 3
72320021024 4 6 1 6 5 2 6 5 5
72320021024 4 7 1 5 5 1 4 4 3
72320021024 5 1 1 4 5 1 3 4 3
72320021024 5 2 1 5 3 2 5 4 5
72320021024 5 3 1 4 6 2 4 2 5
72320021024 5 4 1 3 5 1 3 4 3
72320021024 5 5 1 5 3 1 4 4 6
72320021024 5 6 1 6 3 2 6 5 5
72320021024 5 7 1 6 2 1 2 6 5
72320021024 6 1 1 4 5 1 3 4 4
72320021024 6 2 1 5 5 1 5 2 5
72320021024 6 3 1 4 6 2 4 3 4
72320021024 6 4 1 3 6 1 4 5 3
72320021024 6 5 1 5 5 1 6 5 5
72320021024 6 6 1 5 5 1 5 4 5
72320021024 6 7 1 6 5 2 6 3 5
72320021024 7 1 1 5 3 2 5 5 5
72320021024 7 2 1 3 5 1 5 3 5
72320021024 7 3 1 5 5 1 5 5 5
72320021024 7 5 1 6 2 1 6 6 6
72320021024 7 6 1 5 4 1 6 6 6
72320021024 7 7 1 5 2 2 5 6 4
72320022623 1 1 1 6 2 3 5 4 5
72320022623 1 2 0 4 6 1 3 5 5
72320022623 1 3 1 7 1 2 7 4 7
72320022623 1 4 1 4 2 2 4 2 4
72320022623 1 5 1 3 5 2 3 2 4
72320022623 1 6 2 5 6 1 5 4 6
72320022623 1 7 0 4 5 2 4 2 5
72320022623 2 1 1 5 3 3 5 7 7
72320022623 2 2 1 5 6 3 5 5 6
72320022623 2 3 1 3 5 1 3 3 3
72320022623 2 4 3 2 6 1 3 3 3
72320022623 2 5 1 3 6 1 4 3 4
72320022623 2 6 1 3 5 1 3 4 5
72320022623 2 7 2 6 5 1 5 4 5
72320022623 3 1 1 5 5 2 5 5 6
72320022623 3 2 1 3 5 2 5 4 5
72320022623 3 3 1 6 3 1 6 4 5
72320022623 3 5 4 4 5 1 4 5 4
72320022623 3 6 1 6 3 1 6 4 6
72320022623 3 7 2 4 5 1 3 4 3
72320022623 4 1 3 6 3 1 5 4 5
72320022623 4 2 1 5 6 1 5 5 6
72320022623 4 3 2 3 7 1 4 4 5
72320022623 4 4 1 3 5 1 4 4 4
72320022623 4 5 2 5 3 1 6 4 6
72320022623 4 6 1 4 5 1 4 5 4
72320022623 4 7 2 6 5 1 5 4 4
72320022623 5 1 2 5 5 2 5 5 6
72320022623 5 2 2 3 6 1 4 4 4
72320022623 5 3 2 4 6 1 4 4 5
72320022623 5 4 2 2 3 1 3 6 6
72320022623 5 5 1 4 5 1 4 4 5
72320022623 5 6 1 6 2 1 6 4 6
72320022623 5 7 2 6 3 1 6 4 5
72320022623 6 1 3 5 3 1 6 4 5
72320022623 6 2 2 5 6 2 4 5 5
72320022623 6 3 2 5 5 1 5 4 6
72320022623 6 4 2 5 3 1 5 3 5
72320022623 6 7 2 5 5 1 5 4 6
72320022623 7 1 3 5 3 1 5 4 3
72320022623 7 3 3 4 5 2 5 3 5
72320022623 7 4 1 5 5 1 5 5 6
72320022623 7 5 2 6 4 2 6 3 5
72320022623 7 6 2 5 3 1 5 3 7
72320022623 7 7 2 6 3 1 5 4 6
72320024131 1 1 1 4 4 1 4 4 5
72320024131 1 2 3 4 6 1 4 4 3
72320024131 1 3 1 3 4 2 5 5 3
72320024131 1 4 1 4 2 2 4 6 5
72320024131 1 5 0 4 3 2 4 6 4
72320024131 1 5 0 NA NA NA NA NA NA
72320024131 1 5 0 NA NA NA NA NA NA
72320024131 1 6 1 3 6 1 3 4 4
72320024131 1 7 2 4 3 1 5 5 4


…with the below (sample) intake questionnaire dataset?

PID Age Gender WeightT1 HeightT1 HeightT1Sure Nationality EmployStat DurationIntake
72320021024 22 1 60.0 180 4 19 4 612
72320022623 21 2 646.0 168 4 19 4 785
72320024131 18 1 91.0 189 3 19 4 819
72420022820 23 2 56.0 177 4 19 4 912
72420025120 19 1 94.0 182 3 19 4 949
72420042431 30 2 59.0 163 4 19 2 700
72720031852 33 2 61.0 166 4 19 4 302
72720040736 18 2 61.0 168 4 19 4 438
72820021202 32 2 748.0 176 4 19 4 1340
72820024423 20 2 55.0 155 4 19 4 1366
72820024423 20 1 NA NA NA 80 NA 27
72820024423 20 1 70.0 180 3 19 4 7167
72820030310 21 2 71.0 178 4 19 4 619
72820032352 25 1 73.0 171 4 19 4 1944
72820033718 23 2 60.0 172 4 19 4 1123
72820035529 21 2 51.0 163 4 19 4 855
72820085429 22 2 643.0 172 3 19 4 819
72920025635 24 2 57.0 170 4 82 4 1151
72920031328 25 2 85.0 175 4 19 4 745
72920032518 19 1 59.0 180 3 19 4 1763
72920035032 22 2 67.0 153 4 19 4 536
72920040831 20 2 61.0 170 4 19 4 557
72920043707 26 1 83.0 185 4 19 4 520
72920095757 24 1 100.0 195 4 19 4 633
72920102052 24 1 932.0 192 4 19 4 683
72920103410 24 2 66.0 162 4 37 4 2040
73020013429 24 2 61.0 174 4 19 4 295
73120044728 21 2 64.0 172 4 19 4 1060
80320022144 20 1 72.0 185 4 19 4 585
80320023306 25 2 529.0 163 4 19 4 1155
80320041808 30 2 67.0 163 3 19 4 727
80320071319 22 1 86.0 189 4 19 4 607
80420094658 22 2 58.0 164 4 19 4 101
80520090346 29 2 47.0 165 3 37 4 337
80520092716 20 2 69.0 168 4 19 4 548
80520095211 23 1 69.0 182 4 37 4 1273
80720074948 26 2 52.0 166 3 19 4 600
80720080335 26 1 81.0 193 3 19 5 718
80720081603 20 2 79.0 175 4 19 4 739
80720090833 29 1 88.0 190 4 19 1 700
81120033400 22 2 51.0 162 4 19 4 926
81120034425 24 1 74.0 174 4 19 4 1177
81120062907 22 2 65.0 164 4 19 2 1934
81920040436 19 1 72.0 178 3 19 4 1509
81920043519 21 2 60.0 177 4 19 4 555
81920045208 19 2 53.0 162 3 19 4 1080
81920061238 20 1 60.0 167 3 19 4 509
81920065834 23 2 73.0 162 4 19 1 646
81920085722 19 2 74.0 156 4 19 4 1372
81920092540 19 2 80.0 173 4 19 4 628
81920114333 26 1 92.0 188 4 19 4 275
82020113601 21 2 53.0 160 4 19 4 698
82220030057 21 2 63.0 179 4 19 4 872
82220031046 22 1 73.0 184 3 19 4 20499
82220031904 29 2 997.0 170 3 19 1 628
82220072202 20 2 68.0 177 4 19 4 844
82220073700 28 2 68.0 180 4 19 4 1445
82220093142 54 2 76.0 162 3 19 7 1739
82420021008 26 2 52.0 164 4 19 4 544
82420022427 24 2 62.0 169 4 19 4 487
82420043801 19 2 60.0 168 4 19 4 1056
82420045015 21 2 105.0 173 3 19 4 824
82520013110 28 2 61.0 165 3 19 4 650
82520014152 23 2 85.0 171 4 19 4 658
82520015252 25 2 83.0 182 4 19 1 709
82520032825 22 2 58.0 167 3 19 4 1696
82520033926 25 1 74.0 176 4 19 4 921
82520081324 28 1 105.0 185 4 19 4 560
82520082650 32 2 61.0 168 3 73 2 469
82620023710 28 2 85.0 165 4 19 2 906
82620025707 34 1 67.0 166 4 97 4 1373
82620062252 25 2 62.0 159 4 19 6 664
82620063720 21 2 76.0 162 3 19 4 827
82720013557 56 2 63.0 158 3 19 2 738
82720020148 26 2 56.0 160 4 19 1 416
82720060221 20 2 66.0 170 4 19 4 579
82720061735 24 1 80.0 184 4 19 4 869
82720081103 48 2 61.0 176 4 19 4 744
82720100116 32 2 73.0 163 4 19 2 528
82720101446 32 2 956.0 173 4 19 6 1506
82720102846 25 2 64.0 168 4 19 1 1577
82820031541 27 1 91.0 185 4 19 4 783
82820042154 23 2 51.0 157 4 19 4 790
82820071538 24 2 59.6 158 4 19 4 2560
82820072940 26 1 57.0 169 4 19 6 800
83120011955 24 2 50.0 167 4 19 2 520
83120013222 23 2 57.0 169 4 19 4 1243
83120015057 24 1 76.0 185 3 19 4 1016
83120022846 22 2 57.0 173 4 19 4 837
83120061208 52 2 79.0 170 4 19 2 491
83120062627 26 2 83.7 169 4 19 1 430
83120071010 22 1 67.8 177 3 19 4 447
83120075740 21 2 65.0 170 4 19 4 533
83120083226 26 2 49.0 1605 4 19 3 1674
83120084155 19 2 49.0 162 3 19 4 848
90120044429 24 2 72.0 166 2 19 3 277
90120063349 30 2 78.0 164 4 19 3 770
90120064745 20 2 58.0 170 4 19 4 1154
90120064745 20 2 58.0 170 4 19 4 820
90120070343 32 2 62.0 178 4 19 2 707


A simple solution is to use the ‘merge’ function of base R. In the code below, ‘x’ refers to the ESM data, ‘y’ refers to the intake data, and ‘all.x = TRUE’ ensures the ESM data will not be lost upon merging. The variable ‘PID’ (i.e., participant identification number) is present in both datasets and is the key that connects them.

datX3 <- merge(x=datX, y=datX2, by="PID", all.x = TRUE)
PID DAY SIG NumCEpi Satisfied MoreGrati NumCBeh ActEnj ActCon ExpEnj Age Gender WeightT1 HeightT1 HeightT1Sure Nationality EmployStat DurationIntake
72320021024 1 1 3 4 5 2 4 4 2 22 1 60 180 4 19 4 612
72320021024 1 2 0 4 5 2 5 3 3 22 1 60 180 4 19 4 612
72320021024 1 3 2 5 5 2 3 6 6 22 1 60 180 4 19 4 612
72320021024 1 4 1 5 6 1 5 5 5 22 1 60 180 4 19 4 612
72320021024 1 5 1 3 6 1 3 5 5 22 1 60 180 4 19 4 612
72320021024 1 7 2 2 1 1 2 4 3 22 1 60 180 4 19 4 612
72320021024 2 1 1 5 2 2 5 5 5 22 1 60 180 4 19 4 612
72320021024 2 2 1 3 4 1 3 6 2 22 1 60 180 4 19 4 612
72320021024 2 3 1 3 6 1 3 2 2 22 1 60 180 4 19 4 612
72320021024 2 4 1 4 5 2 5 4 6 22 1 60 180 4 19 4 612
72320021024 2 5 1 4 5 1 2 4 3 22 1 60 180 4 19 4 612
72320021024 2 6 1 3 6 2 3 4 4 22 1 60 180 4 19 4 612
72320021024 2 7 1 3 5 2 4 4 3 22 1 60 180 4 19 4 612
72320021024 3 2 1 5 5 2 5 4 5 22 1 60 180 4 19 4 612
72320021024 3 3 1 4 5 2 4 5 5 22 1 60 180 4 19 4 612
72320021024 3 4 0 4 6 2 5 5 4 22 1 60 180 4 19 4 612
72320021024 3 5 1 4 5 1 4 3 3 22 1 60 180 4 19 4 612
72320021024 3 6 1 5 4 1 5 6 5 22 1 60 180 4 19 4 612
72320021024 3 7 1 5 5 2 4 5 4 22 1 60 180 4 19 4 612
72320021024 4 1 1 5 3 1 5 4 5 22 1 60 180 4 19 4 612
72320021024 4 2 1 4 5 1 3 6 5 22 1 60 180 4 19 4 612
72320021024 4 3 1 3 3 1 4 4 3 22 1 60 180 4 19 4 612
72320021024 4 4 1 4 5 1 4 3 4 22 1 60 180 4 19 4 612
72320021024 4 5 1 4 6 1 4 5 3 22 1 60 180 4 19 4 612
72320021024 4 6 1 6 5 2 6 5 5 22 1 60 180 4 19 4 612
72320021024 4 7 1 5 5 1 4 4 3 22 1 60 180 4 19 4 612
72320021024 5 1 1 4 5 1 3 4 3 22 1 60 180 4 19 4 612
72320021024 5 2 1 5 3 2 5 4 5 22 1 60 180 4 19 4 612
72320021024 5 3 1 4 6 2 4 2 5 22 1 60 180 4 19 4 612
72320021024 5 4 1 3 5 1 3 4 3 22 1 60 180 4 19 4 612
72320021024 5 5 1 5 3 1 4 4 6 22 1 60 180 4 19 4 612
72320021024 5 6 1 6 3 2 6 5 5 22 1 60 180 4 19 4 612
72320021024 5 7 1 6 2 1 2 6 5 22 1 60 180 4 19 4 612
72320021024 6 1 1 4 5 1 3 4 4 22 1 60 180 4 19 4 612
72320021024 6 2 1 5 5 1 5 2 5 22 1 60 180 4 19 4 612
72320021024 6 3 1 4 6 2 4 3 4 22 1 60 180 4 19 4 612
72320021024 6 4 1 3 6 1 4 5 3 22 1 60 180 4 19 4 612
72320021024 6 5 1 5 5 1 6 5 5 22 1 60 180 4 19 4 612
72320021024 6 6 1 5 5 1 5 4 5 22 1 60 180 4 19 4 612
72320021024 6 7 1 6 5 2 6 3 5 22 1 60 180 4 19 4 612
72320021024 7 1 1 5 3 2 5 5 5 22 1 60 180 4 19 4 612
72320021024 7 2 1 3 5 1 5 3 5 22 1 60 180 4 19 4 612
72320021024 7 3 1 5 5 1 5 5 5 22 1 60 180 4 19 4 612
72320021024 7 5 1 6 2 1 6 6 6 22 1 60 180 4 19 4 612
72320021024 7 6 1 5 4 1 6 6 6 22 1 60 180 4 19 4 612
72320021024 7 7 1 5 2 2 5 6 4 22 1 60 180 4 19 4 612
72320022623 1 1 1 6 2 3 5 4 5 21 2 646 168 4 19 4 785
72320022623 1 2 0 4 6 1 3 5 5 21 2 646 168 4 19 4 785
72320022623 1 3 1 7 1 2 7 4 7 21 2 646 168 4 19 4 785
72320022623 1 4 1 4 2 2 4 2 4 21 2 646 168 4 19 4 785
72320022623 1 5 1 3 5 2 3 2 4 21 2 646 168 4 19 4 785
72320022623 1 6 2 5 6 1 5 4 6 21 2 646 168 4 19 4 785
72320022623 1 7 0 4 5 2 4 2 5 21 2 646 168 4 19 4 785
72320022623 2 1 1 5 3 3 5 7 7 21 2 646 168 4 19 4 785
72320022623 2 2 1 5 6 3 5 5 6 21 2 646 168 4 19 4 785
72320022623 2 3 1 3 5 1 3 3 3 21 2 646 168 4 19 4 785
72320022623 2 4 3 2 6 1 3 3 3 21 2 646 168 4 19 4 785
72320022623 2 5 1 3 6 1 4 3 4 21 2 646 168 4 19 4 785
72320022623 2 6 1 3 5 1 3 4 5 21 2 646 168 4 19 4 785
72320022623 2 7 2 6 5 1 5 4 5 21 2 646 168 4 19 4 785
72320022623 3 1 1 5 5 2 5 5 6 21 2 646 168 4 19 4 785
72320022623 3 2 1 3 5 2 5 4 5 21 2 646 168 4 19 4 785
72320022623 3 3 1 6 3 1 6 4 5 21 2 646 168 4 19 4 785
72320022623 3 5 4 4 5 1 4 5 4 21 2 646 168 4 19 4 785
72320022623 3 6 1 6 3 1 6 4 6 21 2 646 168 4 19 4 785
72320022623 3 7 2 4 5 1 3 4 3 21 2 646 168 4 19 4 785
72320022623 4 1 3 6 3 1 5 4 5 21 2 646 168 4 19 4 785
72320022623 4 2 1 5 6 1 5 5 6 21 2 646 168 4 19 4 785
72320022623 4 3 2 3 7 1 4 4 5 21 2 646 168 4 19 4 785
72320022623 4 4 1 3 5 1 4 4 4 21 2 646 168 4 19 4 785

Removing columns and rows via set criteria

The need to remove rows and columns/variables is commonplace in most research. However, it is particularly commonplace in ESM research where numerous variables are often measured for exploratory purposes and won’t make it into the final polished dataset. Also, ESM approaches can be prone to producing poor quality data that will ultimately need removing to find a signal amidst the error.

Lets say I want to remove a column from the table above - say, participants reported bodyweight at the beginning of the data collection period (i.e., the variable ‘WeightT1’). The below code would suffice for this.

datX3$WeightT1 <- NULL 
PID DAY SIG NumCEpi Satisfied MoreGrati NumCBeh ActEnj ActCon ExpEnj Age Gender HeightT1 HeightT1Sure Nationality EmployStat DurationIntake
72320021024 1 1 3 4 5 2 4 4 2 22 1 180 4 19 4 612
72320021024 1 2 0 4 5 2 5 3 3 22 1 180 4 19 4 612
72320021024 1 3 2 5 5 2 3 6 6 22 1 180 4 19 4 612
72320021024 1 4 1 5 6 1 5 5 5 22 1 180 4 19 4 612
72320021024 1 5 1 3 6 1 3 5 5 22 1 180 4 19 4 612
72320021024 1 7 2 2 1 1 2 4 3 22 1 180 4 19 4 612
72320021024 2 1 1 5 2 2 5 5 5 22 1 180 4 19 4 612
72320021024 2 2 1 3 4 1 3 6 2 22 1 180 4 19 4 612
72320021024 2 3 1 3 6 1 3 2 2 22 1 180 4 19 4 612
72320021024 2 4 1 4 5 2 5 4 6 22 1 180 4 19 4 612
72320021024 2 5 1 4 5 1 2 4 3 22 1 180 4 19 4 612
72320021024 2 6 1 3 6 2 3 4 4 22 1 180 4 19 4 612
72320021024 2 7 1 3 5 2 4 4 3 22 1 180 4 19 4 612
72320021024 3 2 1 5 5 2 5 4 5 22 1 180 4 19 4 612
72320021024 3 3 1 4 5 2 4 5 5 22 1 180 4 19 4 612
72320021024 3 4 0 4 6 2 5 5 4 22 1 180 4 19 4 612
72320021024 3 5 1 4 5 1 4 3 3 22 1 180 4 19 4 612
72320021024 3 6 1 5 4 1 5 6 5 22 1 180 4 19 4 612
72320021024 3 7 1 5 5 2 4 5 4 22 1 180 4 19 4 612
72320021024 4 1 1 5 3 1 5 4 5 22 1 180 4 19 4 612
72320021024 4 2 1 4 5 1 3 6 5 22 1 180 4 19 4 612
72320021024 4 3 1 3 3 1 4 4 3 22 1 180 4 19 4 612
72320021024 4 4 1 4 5 1 4 3 4 22 1 180 4 19 4 612
72320021024 4 5 1 4 6 1 4 5 3 22 1 180 4 19 4 612
72320021024 4 6 1 6 5 2 6 5 5 22 1 180 4 19 4 612
72320021024 4 7 1 5 5 1 4 4 3 22 1 180 4 19 4 612
72320021024 5 1 1 4 5 1 3 4 3 22 1 180 4 19 4 612
72320021024 5 2 1 5 3 2 5 4 5 22 1 180 4 19 4 612
72320021024 5 3 1 4 6 2 4 2 5 22 1 180 4 19 4 612
72320021024 5 4 1 3 5 1 3 4 3 22 1 180 4 19 4 612
72320021024 5 5 1 5 3 1 4 4 6 22 1 180 4 19 4 612
72320021024 5 6 1 6 3 2 6 5 5 22 1 180 4 19 4 612
72320021024 5 7 1 6 2 1 2 6 5 22 1 180 4 19 4 612
72320021024 6 1 1 4 5 1 3 4 4 22 1 180 4 19 4 612
72320021024 6 2 1 5 5 1 5 2 5 22 1 180 4 19 4 612
72320021024 6 3 1 4 6 2 4 3 4 22 1 180 4 19 4 612
72320021024 6 4 1 3 6 1 4 5 3 22 1 180 4 19 4 612
72320021024 6 5 1 5 5 1 6 5 5 22 1 180 4 19 4 612
72320021024 6 6 1 5 5 1 5 4 5 22 1 180 4 19 4 612
72320021024 6 7 1 6 5 2 6 3 5 22 1 180 4 19 4 612
72320021024 7 1 1 5 3 2 5 5 5 22 1 180 4 19 4 612
72320021024 7 2 1 3 5 1 5 3 5 22 1 180 4 19 4 612
72320021024 7 3 1 5 5 1 5 5 5 22 1 180 4 19 4 612
72320021024 7 5 1 6 2 1 6 6 6 22 1 180 4 19 4 612
72320021024 7 6 1 5 4 1 6 6 6 22 1 180 4 19 4 612
72320021024 7 7 1 5 2 2 5 6 4 22 1 180 4 19 4 612
72320022623 1 1 1 6 2 3 5 4 5 21 2 168 4 19 4 785
72320022623 1 2 0 4 6 1 3 5 5 21 2 168 4 19 4 785
72320022623 1 3 1 7 1 2 7 4 7 21 2 168 4 19 4 785
72320022623 1 4 1 4 2 2 4 2 4 21 2 168 4 19 4 785
72320022623 1 5 1 3 5 2 3 2 4 21 2 168 4 19 4 785
72320022623 1 6 2 5 6 1 5 4 6 21 2 168 4 19 4 785
72320022623 1 7 0 4 5 2 4 2 5 21 2 168 4 19 4 785
72320022623 2 1 1 5 3 3 5 7 7 21 2 168 4 19 4 785
72320022623 2 2 1 5 6 3 5 5 6 21 2 168 4 19 4 785
72320022623 2 3 1 3 5 1 3 3 3 21 2 168 4 19 4 785
72320022623 2 4 3 2 6 1 3 3 3 21 2 168 4 19 4 785
72320022623 2 5 1 3 6 1 4 3 4 21 2 168 4 19 4 785
72320022623 2 6 1 3 5 1 3 4 5 21 2 168 4 19 4 785
72320022623 2 7 2 6 5 1 5 4 5 21 2 168 4 19 4 785
72320022623 3 1 1 5 5 2 5 5 6 21 2 168 4 19 4 785
72320022623 3 2 1 3 5 2 5 4 5 21 2 168 4 19 4 785
72320022623 3 3 1 6 3 1 6 4 5 21 2 168 4 19 4 785
72320022623 3 5 4 4 5 1 4 5 4 21 2 168 4 19 4 785
72320022623 3 6 1 6 3 1 6 4 6 21 2 168 4 19 4 785
72320022623 3 7 2 4 5 1 3 4 3 21 2 168 4 19 4 785
72320022623 4 1 3 6 3 1 5 4 5 21 2 168 4 19 4 785
72320022623 4 2 1 5 6 1 5 5 6 21 2 168 4 19 4 785
72320022623 4 3 2 3 7 1 4 4 5 21 2 168 4 19 4 785
72320022623 4 4 1 3 5 1 4 4 4 21 2 168 4 19 4 785


If I want to remove rows from a dataframe, I will need a different approach because removal will generally be based upon set criteria. Thus, if I want to remove rows where the variable ‘NumCEpi’ is ‘0’, I would instead opt for using the below code.

datX5 <-datX4[!(datX4$NumCEpi == 0),]
PID DAY SIG NumCEpi Satisfied MoreGrati NumCBeh ActEnj ActCon ExpEnj Age Gender HeightT1 HeightT1Sure Nationality EmployStat DurationIntake
72320021024 1 1 3 4 5 2 4 4 2 22 1 180 4 19 4 612
72320021024 1 3 2 5 5 2 3 6 6 22 1 180 4 19 4 612
72320021024 1 4 1 5 6 1 5 5 5 22 1 180 4 19 4 612
72320021024 1 5 1 3 6 1 3 5 5 22 1 180 4 19 4 612
72320021024 1 7 2 2 1 1 2 4 3 22 1 180 4 19 4 612
72320021024 2 1 1 5 2 2 5 5 5 22 1 180 4 19 4 612
72320021024 2 2 1 3 4 1 3 6 2 22 1 180 4 19 4 612
72320021024 2 3 1 3 6 1 3 2 2 22 1 180 4 19 4 612
72320021024 2 4 1 4 5 2 5 4 6 22 1 180 4 19 4 612
72320021024 2 5 1 4 5 1 2 4 3 22 1 180 4 19 4 612
72320021024 2 6 1 3 6 2 3 4 4 22 1 180 4 19 4 612
72320021024 2 7 1 3 5 2 4 4 3 22 1 180 4 19 4 612
72320021024 3 2 1 5 5 2 5 4 5 22 1 180 4 19 4 612
72320021024 3 3 1 4 5 2 4 5 5 22 1 180 4 19 4 612
72320021024 3 5 1 4 5 1 4 3 3 22 1 180 4 19 4 612
72320021024 3 6 1 5 4 1 5 6 5 22 1 180 4 19 4 612
72320021024 3 7 1 5 5 2 4 5 4 22 1 180 4 19 4 612
72320021024 4 1 1 5 3 1 5 4 5 22 1 180 4 19 4 612
72320021024 4 2 1 4 5 1 3 6 5 22 1 180 4 19 4 612
72320021024 4 3 1 3 3 1 4 4 3 22 1 180 4 19 4 612
72320021024 4 4 1 4 5 1 4 3 4 22 1 180 4 19 4 612
72320021024 4 5 1 4 6 1 4 5 3 22 1 180 4 19 4 612
72320021024 4 6 1 6 5 2 6 5 5 22 1 180 4 19 4 612
72320021024 4 7 1 5 5 1 4 4 3 22 1 180 4 19 4 612
72320021024 5 1 1 4 5 1 3 4 3 22 1 180 4 19 4 612
72320021024 5 2 1 5 3 2 5 4 5 22 1 180 4 19 4 612
72320021024 5 3 1 4 6 2 4 2 5 22 1 180 4 19 4 612
72320021024 5 4 1 3 5 1 3 4 3 22 1 180 4 19 4 612
72320021024 5 5 1 5 3 1 4 4 6 22 1 180 4 19 4 612
72320021024 5 6 1 6 3 2 6 5 5 22 1 180 4 19 4 612
72320021024 5 7 1 6 2 1 2 6 5 22 1 180 4 19 4 612
72320021024 6 1 1 4 5 1 3 4 4 22 1 180 4 19 4 612
72320021024 6 2 1 5 5 1 5 2 5 22 1 180 4 19 4 612
72320021024 6 3 1 4 6 2 4 3 4 22 1 180 4 19 4 612
72320021024 6 4 1 3 6 1 4 5 3 22 1 180 4 19 4 612
72320021024 6 5 1 5 5 1 6 5 5 22 1 180 4 19 4 612
72320021024 6 6 1 5 5 1 5 4 5 22 1 180 4 19 4 612
72320021024 6 7 1 6 5 2 6 3 5 22 1 180 4 19 4 612
72320021024 7 1 1 5 3 2 5 5 5 22 1 180 4 19 4 612
72320021024 7 2 1 3 5 1 5 3 5 22 1 180 4 19 4 612
72320021024 7 3 1 5 5 1 5 5 5 22 1 180 4 19 4 612
72320021024 7 5 1 6 2 1 6 6 6 22 1 180 4 19 4 612
72320021024 7 6 1 5 4 1 6 6 6 22 1 180 4 19 4 612
72320021024 7 7 1 5 2 2 5 6 4 22 1 180 4 19 4 612
72320022623 1 1 1 6 2 3 5 4 5 21 2 168 4 19 4 785
72320022623 1 3 1 7 1 2 7 4 7 21 2 168 4 19 4 785
72320022623 1 4 1 4 2 2 4 2 4 21 2 168 4 19 4 785
72320022623 1 5 1 3 5 2 3 2 4 21 2 168 4 19 4 785
72320022623 1 6 2 5 6 1 5 4 6 21 2 168 4 19 4 785
72320022623 2 1 1 5 3 3 5 7 7 21 2 168 4 19 4 785
72320022623 2 2 1 5 6 3 5 5 6 21 2 168 4 19 4 785
72320022623 2 3 1 3 5 1 3 3 3 21 2 168 4 19 4 785
72320022623 2 4 3 2 6 1 3 3 3 21 2 168 4 19 4 785
72320022623 2 5 1 3 6 1 4 3 4 21 2 168 4 19 4 785
72320022623 2 6 1 3 5 1 3 4 5 21 2 168 4 19 4 785
72320022623 2 7 2 6 5 1 5 4 5 21 2 168 4 19 4 785
72320022623 3 1 1 5 5 2 5 5 6 21 2 168 4 19 4 785
72320022623 3 2 1 3 5 2 5 4 5 21 2 168 4 19 4 785
72320022623 3 3 1 6 3 1 6 4 5 21 2 168 4 19 4 785
72320022623 3 5 4 4 5 1 4 5 4 21 2 168 4 19 4 785
72320022623 3 6 1 6 3 1 6 4 6 21 2 168 4 19 4 785
72320022623 3 7 2 4 5 1 3 4 3 21 2 168 4 19 4 785
72320022623 4 1 3 6 3 1 5 4 5 21 2 168 4 19 4 785
72320022623 4 2 1 5 6 1 5 5 6 21 2 168 4 19 4 785
72320022623 4 3 2 3 7 1 4 4 5 21 2 168 4 19 4 785
72320022623 4 4 1 3 5 1 4 4 4 21 2 168 4 19 4 785
72320022623 4 5 2 5 3 1 6 4 6 21 2 168 4 19 4 785
72320022623 4 6 1 4 5 1 4 5 4 21 2 168 4 19 4 785
72320022623 4 7 2 6 5 1 5 4 4 21 2 168 4 19 4 785
72320022623 5 1 2 5 5 2 5 5 6 21 2 168 4 19 4 785

Remove duplicate data

It often results in ESM research that participants complete more than one questionnaire from a questionnaire link sent to them. Generally this is not wanted, thus this duplicate data will need removing. You can do this using the ‘duplicated’ function of base R.

For instance, if you scroll to the bottom of the table below, you will see participant ‘72420025120’ completed the third questionnaire sent to them on day 5 of the ESM phase twice (i.e., for this participant two rows contain DAY = 5 and SIG = 3).

PID DAY SIG NumCEpi Satisfied MoreGrati NumCBeh ActEnj ActCon ExpEnj Age Gender HeightT1 HeightT1Sure Nationality EmployStat DurationIntake
72320021024 1 1 3 4 5 2 4 4 2 22 1 180 4 19 4 612
72320021024 1 3 2 5 5 2 3 6 6 22 1 180 4 19 4 612
72320021024 1 4 1 5 6 1 5 5 5 22 1 180 4 19 4 612
72320021024 1 5 1 3 6 1 3 5 5 22 1 180 4 19 4 612
72320021024 1 7 2 2 1 1 2 4 3 22 1 180 4 19 4 612
72320021024 2 1 1 5 2 2 5 5 5 22 1 180 4 19 4 612
72320021024 2 2 1 3 4 1 3 6 2 22 1 180 4 19 4 612
72320021024 2 3 1 3 6 1 3 2 2 22 1 180 4 19 4 612
72320021024 2 4 1 4 5 2 5 4 6 22 1 180 4 19 4 612
72320021024 2 5 1 4 5 1 2 4 3 22 1 180 4 19 4 612
72320021024 2 6 1 3 6 2 3 4 4 22 1 180 4 19 4 612
72320021024 2 7 1 3 5 2 4 4 3 22 1 180 4 19 4 612
72320021024 3 2 1 5 5 2 5 4 5 22 1 180 4 19 4 612
72320021024 3 3 1 4 5 2 4 5 5 22 1 180 4 19 4 612
72320021024 3 5 1 4 5 1 4 3 3 22 1 180 4 19 4 612
72320021024 3 6 1 5 4 1 5 6 5 22 1 180 4 19 4 612
72320021024 3 7 1 5 5 2 4 5 4 22 1 180 4 19 4 612
72320021024 4 1 1 5 3 1 5 4 5 22 1 180 4 19 4 612
72320021024 4 2 1 4 5 1 3 6 5 22 1 180 4 19 4 612
72320021024 4 3 1 3 3 1 4 4 3 22 1 180 4 19 4 612
72320021024 4 4 1 4 5 1 4 3 4 22 1 180 4 19 4 612
72320021024 4 5 1 4 6 1 4 5 3 22 1 180 4 19 4 612
72320021024 4 6 1 6 5 2 6 5 5 22 1 180 4 19 4 612
72320021024 4 7 1 5 5 1 4 4 3 22 1 180 4 19 4 612
72320021024 5 1 1 4 5 1 3 4 3 22 1 180 4 19 4 612
72320021024 5 2 1 5 3 2 5 4 5 22 1 180 4 19 4 612
72320021024 5 3 1 4 6 2 4 2 5 22 1 180 4 19 4 612
72320021024 5 4 1 3 5 1 3 4 3 22 1 180 4 19 4 612
72320021024 5 5 1 5 3 1 4 4 6 22 1 180 4 19 4 612
72320021024 5 6 1 6 3 2 6 5 5 22 1 180 4 19 4 612
72320021024 5 7 1 6 2 1 2 6 5 22 1 180 4 19 4 612
72320021024 6 1 1 4 5 1 3 4 4 22 1 180 4 19 4 612
72320021024 6 2 1 5 5 1 5 2 5 22 1 180 4 19 4 612
72320021024 6 3 1 4 6 2 4 3 4 22 1 180 4 19 4 612
72320021024 6 4 1 3 6 1 4 5 3 22 1 180 4 19 4 612
72320021024 6 5 1 5 5 1 6 5 5 22 1 180 4 19 4 612
72320021024 6 6 1 5 5 1 5 4 5 22 1 180 4 19 4 612
72320021024 6 7 1 6 5 2 6 3 5 22 1 180 4 19 4 612
72320021024 7 1 1 5 3 2 5 5 5 22 1 180 4 19 4 612
72320021024 7 2 1 3 5 1 5 3 5 22 1 180 4 19 4 612
72320021024 7 3 1 5 5 1 5 5 5 22 1 180 4 19 4 612
72320021024 7 5 1 6 2 1 6 6 6 22 1 180 4 19 4 612
72320021024 7 6 1 5 4 1 6 6 6 22 1 180 4 19 4 612
72320021024 7 7 1 5 2 2 5 6 4 22 1 180 4 19 4 612
72320022623 1 1 1 6 2 3 5 4 5 21 2 168 4 19 4 785
72320022623 1 3 1 7 1 2 7 4 7 21 2 168 4 19 4 785
72320022623 1 4 1 4 2 2 4 2 4 21 2 168 4 19 4 785
72320022623 1 5 1 3 5 2 3 2 4 21 2 168 4 19 4 785
72320022623 1 6 2 5 6 1 5 4 6 21 2 168 4 19 4 785
72320022623 2 1 1 5 3 3 5 7 7 21 2 168 4 19 4 785
72320022623 2 2 1 5 6 3 5 5 6 21 2 168 4 19 4 785
72320022623 2 3 1 3 5 1 3 3 3 21 2 168 4 19 4 785
72320022623 2 4 3 2 6 1 3 3 3 21 2 168 4 19 4 785
72320022623 2 5 1 3 6 1 4 3 4 21 2 168 4 19 4 785
72320022623 2 6 1 3 5 1 3 4 5 21 2 168 4 19 4 785
72320022623 2 7 2 6 5 1 5 4 5 21 2 168 4 19 4 785
72320022623 3 1 1 5 5 2 5 5 6 21 2 168 4 19 4 785
72320022623 3 2 1 3 5 2 5 4 5 21 2 168 4 19 4 785
72320022623 3 3 1 6 3 1 6 4 5 21 2 168 4 19 4 785
72320022623 3 5 4 4 5 1 4 5 4 21 2 168 4 19 4 785
72320022623 3 6 1 6 3 1 6 4 6 21 2 168 4 19 4 785
72320022623 3 7 2 4 5 1 3 4 3 21 2 168 4 19 4 785
72320022623 4 1 3 6 3 1 5 4 5 21 2 168 4 19 4 785
72320022623 4 2 1 5 6 1 5 5 6 21 2 168 4 19 4 785
72320022623 4 3 2 3 7 1 4 4 5 21 2 168 4 19 4 785
72320022623 4 4 1 3 5 1 4 4 4 21 2 168 4 19 4 785
72320022623 4 5 2 5 3 1 6 4 6 21 2 168 4 19 4 785
72320022623 4 6 1 4 5 1 4 5 4 21 2 168 4 19 4 785
72320022623 4 7 2 6 5 1 5 4 4 21 2 168 4 19 4 785
72320022623 5 1 2 5 5 2 5 5 6 21 2 168 4 19 4 785
72320022623 5 2 2 3 6 1 4 4 4 21 2 168 4 19 4 785
72320022623 5 3 2 4 6 1 4 4 5 21 2 168 4 19 4 785
72320022623 5 4 2 2 3 1 3 6 6 21 2 168 4 19 4 785
72320022623 5 5 1 4 5 1 4 4 5 21 2 168 4 19 4 785
72320022623 5 6 1 6 2 1 6 4 6 21 2 168 4 19 4 785
72320022623 5 7 2 6 3 1 6 4 5 21 2 168 4 19 4 785
72320022623 6 1 3 5 3 1 6 4 5 21 2 168 4 19 4 785
72320022623 6 2 2 5 6 2 4 5 5 21 2 168 4 19 4 785
72320022623 6 3 2 5 5 1 5 4 6 21 2 168 4 19 4 785
72320022623 6 4 2 5 3 1 5 3 5 21 2 168 4 19 4 785
72320022623 6 7 2 5 5 1 5 4 6 21 2 168 4 19 4 785
72320022623 7 1 3 5 3 1 5 4 3 21 2 168 4 19 4 785
72320022623 7 3 3 4 5 2 5 3 5 21 2 168 4 19 4 785
72320022623 7 4 1 5 5 1 5 5 6 21 2 168 4 19 4 785
72320022623 7 5 2 6 4 2 6 3 5 21 2 168 4 19 4 785
72320022623 7 6 2 5 3 1 5 3 7 21 2 168 4 19 4 785
72320022623 7 7 2 6 3 1 5 4 6 21 2 168 4 19 4 785
72320024131 1 1 1 4 4 1 4 4 5 18 1 189 3 19 4 819
72320024131 1 2 3 4 6 1 4 4 3 18 1 189 3 19 4 819
72320024131 1 3 1 3 4 2 5 5 3 18 1 189 3 19 4 819
72320024131 1 4 1 4 2 2 4 6 5 18 1 189 3 19 4 819
72320024131 1 6 1 3 6 1 3 4 4 18 1 189 3 19 4 819
72320024131 1 7 2 4 3 1 5 5 4 18 1 189 3 19 4 819
72320024131 2 1 2 4 5 2 3 3 5 18 1 189 3 19 4 819
72320024131 2 3 1 5 5 1 6 5 4 18 1 189 3 19 4 819
72320024131 2 4 1 5 4 2 5 5 4 18 1 189 3 19 4 819
72320024131 2 5 2 3 3 1 4 7 4 18 1 189 3 19 4 819
72320024131 2 6 2 5 4 2 5 4 5 18 1 189 3 19 4 819
72320024131 2 7 1 NA NA NA NA NA NA 18 1 189 3 19 4 819
72320024131 3 1 4 4 4 2 4 3 4 18 1 189 3 19 4 819
72320024131 3 2 2 5 5 1 5 4 5 18 1 189 3 19 4 819
72320024131 3 3 1 5 5 1 5 5 5 18 1 189 3 19 4 819
72320024131 3 4 1 3 5 1 4 3 4 18 1 189 3 19 4 819
72320024131 3 5 1 5 5 2 6 3 5 18 1 189 3 19 4 819
72320024131 3 7 4 5 1 2 4 3 2 18 1 189 3 19 4 819
72320024131 4 1 2 5 2 2 5 3 5 18 1 189 3 19 4 819
72320024131 4 3 2 NA NA 1 NA NA NA 18 1 189 3 19 4 819
72320024131 4 3 2 6 5 1 6 5 5 18 1 189 3 19 4 819
72320024131 4 4 1 5 3 2 5 5 4 18 1 189 3 19 4 819
72320024131 4 5 1 3 5 1 3 3 2 18 1 189 3 19 4 819
72320024131 4 6 3 5 4 2 5 4 5 18 1 189 3 19 4 819
72320024131 4 7 1 4 3 2 5 3 5 18 1 189 3 19 4 819
72320024131 5 2 3 3 2 2 4 2 4 18 1 189 3 19 4 819
72320024131 5 3 1 4 4 1 3 4 3 18 1 189 3 19 4 819
72320024131 5 4 1 5 4 1 4 3 5 18 1 189 3 19 4 819
72320024131 5 6 1 6 6 2 5 4 5 18 1 189 3 19 4 819
72320024131 5 7 1 5 4 1 5 3 4 18 1 189 3 19 4 819
72320024131 6 1 2 4 5 1 5 2 5 18 1 189 3 19 4 819
72320024131 6 4 2 5 5 2 5 4 5 18 1 189 3 19 4 819
72320024131 6 5 2 4 4 2 5 4 4 18 1 189 3 19 4 819
72320024131 6 6 2 4 4 1 5 3 4 18 1 189 3 19 4 819
72320024131 6 7 1 5 3 2 5 3 5 18 1 189 3 19 4 819
72320024131 7 1 3 5 4 2 5 4 5 18 1 189 3 19 4 819
72320024131 7 2 1 4 4 1 4 3 4 18 1 189 3 19 4 819
72320024131 7 3 3 5 4 1 6 4 5 18 1 189 3 19 4 819
72320024131 7 4 1 4 5 1 4 4 4 18 1 189 3 19 4 819
72320024131 7 5 3 5 4 1 5 3 5 18 1 189 3 19 4 819
72320024131 7 6 1 4 4 2 4 2 5 18 1 189 3 19 4 819
72320024131 7 7 1 4 5 1 4 4 5 18 1 189 3 19 4 819
72420022820 1 3 2 4 3 1 4 3 4 23 2 177 4 19 4 912
72420022820 1 4 1 3 6 2 3 2 3 23 2 177 4 19 4 912
72420022820 1 5 1 4 5 1 5 4 6 23 2 177 4 19 4 912
72420022820 1 6 3 5 2 2 5 6 5 23 2 177 4 19 4 912
72420022820 2 2 3 5 5 2 5 3 4 23 2 177 4 19 4 912
72420022820 2 3 1 3 5 1 4 6 2 23 2 177 4 19 4 912
72420022820 2 4 1 6 3 2 6 5 7 23 2 177 4 19 4 912
72420022820 2 5 3 6 5 1 6 4 7 23 2 177 4 19 4 912
72420022820 2 6 1 3 5 1 5 6 3 23 2 177 4 19 4 912
72420022820 2 7 3 4 5 1 3 6 2 23 2 177 4 19 4 912
72420022820 3 1 1 5 6 1 4 4 4 23 2 177 4 19 4 912
72420022820 3 2 2 6 5 1 6 4 6 23 2 177 4 19 4 912
72420022820 3 3 2 3 5 1 4 5 3 23 2 177 4 19 4 912
72420022820 3 4 1 5 7 2 6 4 5 23 2 177 4 19 4 912
72420022820 3 5 1 6 6 1 7 6 6 23 2 177 4 19 4 912
72420022820 3 6 1 2 5 1 3 5 2 23 2 177 4 19 4 912
72420022820 3 7 1 5 5 1 4 5 6 23 2 177 4 19 4 912
72420022820 4 1 3 3 6 2 2 5 3 23 2 177 4 19 4 912
72420022820 4 2 1 4 5 1 4 3 2 23 2 177 4 19 4 912
72420022820 4 3 2 5 5 1 5 3 5 23 2 177 4 19 4 912
72420022820 4 4 2 2 5 1 3 6 2 23 2 177 4 19 4 912
72420022820 4 5 1 2 5 2 5 4 5 23 2 177 4 19 4 912
72420022820 4 6 1 4 5 1 5 4 5 23 2 177 4 19 4 912
72420022820 4 7 2 3 6 1 4 6 2 23 2 177 4 19 4 912
72420022820 5 1 3 5 6 1 5 4 5 23 2 177 4 19 4 912
72420022820 5 2 1 3 6 1 3 5 2 23 2 177 4 19 4 912
72420022820 5 3 1 5 6 2 5 5 3 23 2 177 4 19 4 912
72420022820 5 4 1 4 5 1 5 3 5 23 2 177 4 19 4 912
72420022820 5 5 2 4 5 1 4 3 3 23 2 177 4 19 4 912
72420022820 5 6 1 5 5 1 4 4 2 23 2 177 4 19 4 912
72420022820 5 7 1 3 4 1 3 4 2 23 2 177 4 19 4 912
72420022820 6 1 3 4 5 1 4 3 5 23 2 177 4 19 4 912
72420022820 6 2 1 3 6 1 4 5 2 23 2 177 4 19 4 912
72420022820 6 3 1 6 6 2 6 5 6 23 2 177 4 19 4 912
72420022820 6 4 1 5 6 1 6 4 6 23 2 177 4 19 4 912
72420022820 6 5 1 4 5 1 4 5 3 23 2 177 4 19 4 912
72420022820 6 6 1 4 5 2 5 3 4 23 2 177 4 19 4 912
72420022820 6 7 3 4 4 2 4 5 5 23 2 177 4 19 4 912
72420022820 7 2 3 5 6 1 5 4 4 23 2 177 4 19 4 912
72420022820 7 3 1 5 5 2 4 5 3 23 2 177 4 19 4 912
72420022820 7 4 1 5 5 1 5 5 4 23 2 177 4 19 4 912
72420022820 7 5 1 4 7 1 5 4 5 23 2 177 4 19 4 912
72420022820 7 6 1 5 5 1 5 6 5 23 2 177 4 19 4 912
72420022820 7 7 1 5 7 1 5 3 7 23 2 177 4 19 4 912
72420025120 1 1 1 5 6 1 3 2 5 19 1 182 3 19 4 949
72420025120 1 2 2 2 6 1 2 1 5 19 1 182 3 19 4 949
72420025120 1 4 3 3 2 4 5 3 2 19 1 182 3 19 4 949
72420025120 1 6 1 6 6 1 6 6 5 19 1 182 3 19 4 949
72420025120 1 7 1 5 2 2 5 4 6 19 1 182 3 19 4 949
72420025120 2 2 1 4 6 2 4 5 6 19 1 182 3 19 4 949
72420025120 2 3 1 5 5 1 5 3 3 19 1 182 3 19 4 949
72420025120 2 4 1 5 3 2 5 3 6 19 1 182 3 19 4 949
72420025120 2 5 2 2 5 2 2 2 6 19 1 182 3 19 4 949
72420025120 2 6 1 3 6 1 2 2 2 19 1 182 3 19 4 949
72420025120 2 7 1 3 6 1 4 5 6 19 1 182 3 19 4 949
72420025120 3 1 4 5 2 2 5 4 2 19 1 182 3 19 4 949
72420025120 3 2 1 3 5 2 2 2 2 19 1 182 3 19 4 949
72420025120 3 3 1 3 3 1 3 3 5 19 1 182 3 19 4 949
72420025120 3 4 1 4 1 1 4 2 2 19 1 182 3 19 4 949
72420025120 3 5 1 2 2 1 2 2 6 19 1 182 3 19 4 949
72420025120 3 6 1 4 1 1 3 4 1 19 1 182 3 19 4 949
72420025120 3 7 1 2 6 1 2 6 5 19 1 182 3 19 4 949
72420025120 4 1 1 3 5 2 3 3 5 19 1 182 3 19 4 949
72420025120 4 2 1 6 5 1 6 6 2 19 1 182 3 19 4 949
72420025120 4 3 1 7 3 1 5 4 3 19 1 182 3 19 4 949
72420025120 4 4 1 5 5 1 6 4 3 19 1 182 3 19 4 949
72420025120 4 6 2 6 4 1 7 2 3 19 1 182 3 19 4 949
72420025120 5 1 1 4 5 2 3 5 5 19 1 182 3 19 4 949
72420025120 5 3 1 3 7 1 3 4 3 19 1 182 3 19 4 949
72420025120 5 3 1 NA NA NA NA NA NA 19 1 182 3 19 4 949
72420025120 5 4 1 5 3 1 5 5 3 19 1 182 3 19 4 949


To remove this duplicate row I can use the below code, which identifies duplicates using the variables ‘PID’, ‘DAY’, and ‘SIG’ (i.e., this function will record a duplicate if it finds a second, third, fourth, etc. row where values for these variables are identical).

datX6 <- datX5[!duplicated(datX5[c('PID', 'DAY', 'SIG')]),]
PID DAY SIG NumCEpi Satisfied MoreGrati NumCBeh ActEnj ActCon ExpEnj Age Gender HeightT1 HeightT1Sure Nationality EmployStat DurationIntake
72320021024 1 1 3 4 5 2 4 4 2 22 1 180 4 19 4 612
72320021024 1 3 2 5 5 2 3 6 6 22 1 180 4 19 4 612
72320021024 1 4 1 5 6 1 5 5 5 22 1 180 4 19 4 612
72320021024 1 5 1 3 6 1 3 5 5 22 1 180 4 19 4 612
72320021024 1 7 2 2 1 1 2 4 3 22 1 180 4 19 4 612
72320021024 2 1 1 5 2 2 5 5 5 22 1 180 4 19 4 612
72320021024 2 2 1 3 4 1 3 6 2 22 1 180 4 19 4 612
72320021024 2 3 1 3 6 1 3 2 2 22 1 180 4 19 4 612
72320021024 2 4 1 4 5 2 5 4 6 22 1 180 4 19 4 612
72320021024 2 5 1 4 5 1 2 4 3 22 1 180 4 19 4 612
72320021024 2 6 1 3 6 2 3 4 4 22 1 180 4 19 4 612
72320021024 2 7 1 3 5 2 4 4 3 22 1 180 4 19 4 612
72320021024 3 2 1 5 5 2 5 4 5 22 1 180 4 19 4 612
72320021024 3 3 1 4 5 2 4 5 5 22 1 180 4 19 4 612
72320021024 3 5 1 4 5 1 4 3 3 22 1 180 4 19 4 612
72320021024 3 6 1 5 4 1 5 6 5 22 1 180 4 19 4 612
72320021024 3 7 1 5 5 2 4 5 4 22 1 180 4 19 4 612
72320021024 4 1 1 5 3 1 5 4 5 22 1 180 4 19 4 612
72320021024 4 2 1 4 5 1 3 6 5 22 1 180 4 19 4 612
72320021024 4 3 1 3 3 1 4 4 3 22 1 180 4 19 4 612
72320021024 4 4 1 4 5 1 4 3 4 22 1 180 4 19 4 612
72320021024 4 5 1 4 6 1 4 5 3 22 1 180 4 19 4 612
72320021024 4 6 1 6 5 2 6 5 5 22 1 180 4 19 4 612
72320021024 4 7 1 5 5 1 4 4 3 22 1 180 4 19 4 612
72320021024 5 1 1 4 5 1 3 4 3 22 1 180 4 19 4 612
72320021024 5 2 1 5 3 2 5 4 5 22 1 180 4 19 4 612
72320021024 5 3 1 4 6 2 4 2 5 22 1 180 4 19 4 612
72320021024 5 4 1 3 5 1 3 4 3 22 1 180 4 19 4 612
72320021024 5 5 1 5 3 1 4 4 6 22 1 180 4 19 4 612
72320021024 5 6 1 6 3 2 6 5 5 22 1 180 4 19 4 612
72320021024 5 7 1 6 2 1 2 6 5 22 1 180 4 19 4 612
72320021024 6 1 1 4 5 1 3 4 4 22 1 180 4 19 4 612
72320021024 6 2 1 5 5 1 5 2 5 22 1 180 4 19 4 612
72320021024 6 3 1 4 6 2 4 3 4 22 1 180 4 19 4 612
72320021024 6 4 1 3 6 1 4 5 3 22 1 180 4 19 4 612
72320021024 6 5 1 5 5 1 6 5 5 22 1 180 4 19 4 612
72320021024 6 6 1 5 5 1 5 4 5 22 1 180 4 19 4 612
72320021024 6 7 1 6 5 2 6 3 5 22 1 180 4 19 4 612
72320021024 7 1 1 5 3 2 5 5 5 22 1 180 4 19 4 612
72320021024 7 2 1 3 5 1 5 3 5 22 1 180 4 19 4 612
72320021024 7 3 1 5 5 1 5 5 5 22 1 180 4 19 4 612
72320021024 7 5 1 6 2 1 6 6 6 22 1 180 4 19 4 612
72320021024 7 6 1 5 4 1 6 6 6 22 1 180 4 19 4 612
72320021024 7 7 1 5 2 2 5 6 4 22 1 180 4 19 4 612
72320022623 1 1 1 6 2 3 5 4 5 21 2 168 4 19 4 785
72320022623 1 3 1 7 1 2 7 4 7 21 2 168 4 19 4 785
72320022623 1 4 1 4 2 2 4 2 4 21 2 168 4 19 4 785
72320022623 1 5 1 3 5 2 3 2 4 21 2 168 4 19 4 785
72320022623 1 6 2 5 6 1 5 4 6 21 2 168 4 19 4 785
72320022623 2 1 1 5 3 3 5 7 7 21 2 168 4 19 4 785
72320022623 2 2 1 5 6 3 5 5 6 21 2 168 4 19 4 785
72320022623 2 3 1 3 5 1 3 3 3 21 2 168 4 19 4 785
72320022623 2 4 3 2 6 1 3 3 3 21 2 168 4 19 4 785
72320022623 2 5 1 3 6 1 4 3 4 21 2 168 4 19 4 785
72320022623 2 6 1 3 5 1 3 4 5 21 2 168 4 19 4 785
72320022623 2 7 2 6 5 1 5 4 5 21 2 168 4 19 4 785
72320022623 3 1 1 5 5 2 5 5 6 21 2 168 4 19 4 785
72320022623 3 2 1 3 5 2 5 4 5 21 2 168 4 19 4 785
72320022623 3 3 1 6 3 1 6 4 5 21 2 168 4 19 4 785
72320022623 3 5 4 4 5 1 4 5 4 21 2 168 4 19 4 785
72320022623 3 6 1 6 3 1 6 4 6 21 2 168 4 19 4 785
72320022623 3 7 2 4 5 1 3 4 3 21 2 168 4 19 4 785
72320022623 4 1 3 6 3 1 5 4 5 21 2 168 4 19 4 785
72320022623 4 2 1 5 6 1 5 5 6 21 2 168 4 19 4 785
72320022623 4 3 2 3 7 1 4 4 5 21 2 168 4 19 4 785
72320022623 4 4 1 3 5 1 4 4 4 21 2 168 4 19 4 785
72320022623 4 5 2 5 3 1 6 4 6 21 2 168 4 19 4 785
72320022623 4 6 1 4 5 1 4 5 4 21 2 168 4 19 4 785
72320022623 4 7 2 6 5 1 5 4 4 21 2 168 4 19 4 785
72320022623 5 1 2 5 5 2 5 5 6 21 2 168 4 19 4 785
72320022623 5 2 2 3 6 1 4 4 4 21 2 168 4 19 4 785
72320022623 5 3 2 4 6 1 4 4 5 21 2 168 4 19 4 785
72320022623 5 4 2 2 3 1 3 6 6 21 2 168 4 19 4 785
72320022623 5 5 1 4 5 1 4 4 5 21 2 168 4 19 4 785
72320022623 5 6 1 6 2 1 6 4 6 21 2 168 4 19 4 785
72320022623 5 7 2 6 3 1 6 4 5 21 2 168 4 19 4 785
72320022623 6 1 3 5 3 1 6 4 5 21 2 168 4 19 4 785
72320022623 6 2 2 5 6 2 4 5 5 21 2 168 4 19 4 785
72320022623 6 3 2 5 5 1 5 4 6 21 2 168 4 19 4 785
72320022623 6 4 2 5 3 1 5 3 5 21 2 168 4 19 4 785
72320022623 6 7 2 5 5 1 5 4 6 21 2 168 4 19 4 785
72320022623 7 1 3 5 3 1 5 4 3 21 2 168 4 19 4 785
72320022623 7 3 3 4 5 2 5 3 5 21 2 168 4 19 4 785
72320022623 7 4 1 5 5 1 5 5 6 21 2 168 4 19 4 785
72320022623 7 5 2 6 4 2 6 3 5 21 2 168 4 19 4 785
72320022623 7 6 2 5 3 1 5 3 7 21 2 168 4 19 4 785
72320022623 7 7 2 6 3 1 5 4 6 21 2 168 4 19 4 785
72320024131 1 1 1 4 4 1 4 4 5 18 1 189 3 19 4 819
72320024131 1 2 3 4 6 1 4 4 3 18 1 189 3 19 4 819
72320024131 1 3 1 3 4 2 5 5 3 18 1 189 3 19 4 819
72320024131 1 4 1 4 2 2 4 6 5 18 1 189 3 19 4 819
72320024131 1 6 1 3 6 1 3 4 4 18 1 189 3 19 4 819
72320024131 1 7 2 4 3 1 5 5 4 18 1 189 3 19 4 819
72320024131 2 1 2 4 5 2 3 3 5 18 1 189 3 19 4 819
72320024131 2 3 1 5 5 1 6 5 4 18 1 189 3 19 4 819
72320024131 2 4 1 5 4 2 5 5 4 18 1 189 3 19 4 819
72320024131 2 5 2 3 3 1 4 7 4 18 1 189 3 19 4 819
72320024131 2 6 2 5 4 2 5 4 5 18 1 189 3 19 4 819
72320024131 2 7 1 NA NA NA NA NA NA 18 1 189 3 19 4 819
72320024131 3 1 4 4 4 2 4 3 4 18 1 189 3 19 4 819
72320024131 3 2 2 5 5 1 5 4 5 18 1 189 3 19 4 819
72320024131 3 3 1 5 5 1 5 5 5 18 1 189 3 19 4 819
72320024131 3 4 1 3 5 1 4 3 4 18 1 189 3 19 4 819
72320024131 3 5 1 5 5 2 6 3 5 18 1 189 3 19 4 819
72320024131 3 7 4 5 1 2 4 3 2 18 1 189 3 19 4 819
72320024131 4 1 2 5 2 2 5 3 5 18 1 189 3 19 4 819
72320024131 4 3 2 NA NA 1 NA NA NA 18 1 189 3 19 4 819
72320024131 4 4 1 5 3 2 5 5 4 18 1 189 3 19 4 819
72320024131 4 5 1 3 5 1 3 3 2 18 1 189 3 19 4 819
72320024131 4 6 3 5 4 2 5 4 5 18 1 189 3 19 4 819
72320024131 4 7 1 4 3 2 5 3 5 18 1 189 3 19 4 819
72320024131 5 2 3 3 2 2 4 2 4 18 1 189 3 19 4 819
72320024131 5 3 1 4 4 1 3 4 3 18 1 189 3 19 4 819
72320024131 5 4 1 5 4 1 4 3 5 18 1 189 3 19 4 819
72320024131 5 6 1 6 6 2 5 4 5 18 1 189 3 19 4 819
72320024131 5 7 1 5 4 1 5 3 4 18 1 189 3 19 4 819
72320024131 6 1 2 4 5 1 5 2 5 18 1 189 3 19 4 819
72320024131 6 4 2 5 5 2 5 4 5 18 1 189 3 19 4 819
72320024131 6 5 2 4 4 2 5 4 4 18 1 189 3 19 4 819
72320024131 6 6 2 4 4 1 5 3 4 18 1 189 3 19 4 819
72320024131 6 7 1 5 3 2 5 3 5 18 1 189 3 19 4 819
72320024131 7 1 3 5 4 2 5 4 5 18 1 189 3 19 4 819
72320024131 7 2 1 4 4 1 4 3 4 18 1 189 3 19 4 819
72320024131 7 3 3 5 4 1 6 4 5 18 1 189 3 19 4 819
72320024131 7 4 1 4 5 1 4 4 4 18 1 189 3 19 4 819
72320024131 7 5 3 5 4 1 5 3 5 18 1 189 3 19 4 819
72320024131 7 6 1 4 4 2 4 2 5 18 1 189 3 19 4 819
72320024131 7 7 1 4 5 1 4 4 5 18 1 189 3 19 4 819
72420022820 1 3 2 4 3 1 4 3 4 23 2 177 4 19 4 912
72420022820 1 4 1 3 6 2 3 2 3 23 2 177 4 19 4 912
72420022820 1 5 1 4 5 1 5 4 6 23 2 177 4 19 4 912
72420022820 1 6 3 5 2 2 5 6 5 23 2 177 4 19 4 912
72420022820 2 2 3 5 5 2 5 3 4 23 2 177 4 19 4 912
72420022820 2 3 1 3 5 1 4 6 2 23 2 177 4 19 4 912
72420022820 2 4 1 6 3 2 6 5 7 23 2 177 4 19 4 912
72420022820 2 5 3 6 5 1 6 4 7 23 2 177 4 19 4 912
72420022820 2 6 1 3 5 1 5 6 3 23 2 177 4 19 4 912
72420022820 2 7 3 4 5 1 3 6 2 23 2 177 4 19 4 912
72420022820 3 1 1 5 6 1 4 4 4 23 2 177 4 19 4 912
72420022820 3 2 2 6 5 1 6 4 6 23 2 177 4 19 4 912
72420022820 3 3 2 3 5 1 4 5 3 23 2 177 4 19 4 912
72420022820 3 4 1 5 7 2 6 4 5 23 2 177 4 19 4 912
72420022820 3 5 1 6 6 1 7 6 6 23 2 177 4 19 4 912
72420022820 3 6 1 2 5 1 3 5 2 23 2 177 4 19 4 912
72420022820 3 7 1 5 5 1 4 5 6 23 2 177 4 19 4 912
72420022820 4 1 3 3 6 2 2 5 3 23 2 177 4 19 4 912
72420022820 4 2 1 4 5 1 4 3 2 23 2 177 4 19 4 912
72420022820 4 3 2 5 5 1 5 3 5 23 2 177 4 19 4 912
72420022820 4 4 2 2 5 1 3 6 2 23 2 177 4 19 4 912
72420022820 4 5 1 2 5 2 5 4 5 23 2 177 4 19 4 912
72420022820 4 6 1 4 5 1 5 4 5 23 2 177 4 19 4 912
72420022820 4 7 2 3 6 1 4 6 2 23 2 177 4 19 4 912
72420022820 5 1 3 5 6 1 5 4 5 23 2 177 4 19 4 912
72420022820 5 2 1 3 6 1 3 5 2 23 2 177 4 19 4 912
72420022820 5 3 1 5 6 2 5 5 3 23 2 177 4 19 4 912
72420022820 5 4 1 4 5 1 5 3 5 23 2 177 4 19 4 912
72420022820 5 5 2 4 5 1 4 3 3 23 2 177 4 19 4 912
72420022820 5 6 1 5 5 1 4 4 2 23 2 177 4 19 4 912
72420022820 5 7 1 3 4 1 3 4 2 23 2 177 4 19 4 912
72420022820 6 1 3 4 5 1 4 3 5 23 2 177 4 19 4 912
72420022820 6 2 1 3 6 1 4 5 2 23 2 177 4 19 4 912
72420022820 6 3 1 6 6 2 6 5 6 23 2 177 4 19 4 912
72420022820 6 4 1 5 6 1 6 4 6 23 2 177 4 19 4 912
72420022820 6 5 1 4 5 1 4 5 3 23 2 177 4 19 4 912
72420022820 6 6 1 4 5 2 5 3 4 23 2 177 4 19 4 912
72420022820 6 7 3 4 4 2 4 5 5 23 2 177 4 19 4 912
72420022820 7 2 3 5 6 1 5 4 4 23 2 177 4 19 4 912
72420022820 7 3 1 5 5 2 4 5 3 23 2 177 4 19 4 912
72420022820 7 4 1 5 5 1 5 5 4 23 2 177 4 19 4 912
72420022820 7 5 1 4 7 1 5 4 5 23 2 177 4 19 4 912
72420022820 7 6 1 5 5 1 5 6 5 23 2 177 4 19 4 912
72420022820 7 7 1 5 7 1 5 3 7 23 2 177 4 19 4 912
72420025120 1 1 1 5 6 1 3 2 5 19 1 182 3 19 4 949
72420025120 1 2 2 2 6 1 2 1 5 19 1 182 3 19 4 949
72420025120 1 4 3 3 2 4 5 3 2 19 1 182 3 19 4 949
72420025120 1 6 1 6 6 1 6 6 5 19 1 182 3 19 4 949
72420025120 1 7 1 5 2 2 5 4 6 19 1 182 3 19 4 949
72420025120 2 2 1 4 6 2 4 5 6 19 1 182 3 19 4 949
72420025120 2 3 1 5 5 1 5 3 3 19 1 182 3 19 4 949
72420025120 2 4 1 5 3 2 5 3 6 19 1 182 3 19 4 949
72420025120 2 5 2 2 5 2 2 2 6 19 1 182 3 19 4 949
72420025120 2 6 1 3 6 1 2 2 2 19 1 182 3 19 4 949
72420025120 2 7 1 3 6 1 4 5 6 19 1 182 3 19 4 949
72420025120 3 1 4 5 2 2 5 4 2 19 1 182 3 19 4 949
72420025120 3 2 1 3 5 2 2 2 2 19 1 182 3 19 4 949
72420025120 3 3 1 3 3 1 3 3 5 19 1 182 3 19 4 949
72420025120 3 4 1 4 1 1 4 2 2 19 1 182 3 19 4 949
72420025120 3 5 1 2 2 1 2 2 6 19 1 182 3 19 4 949
72420025120 3 6 1 4 1 1 3 4 1 19 1 182 3 19 4 949
72420025120 3 7 1 2 6 1 2 6 5 19 1 182 3 19 4 949
72420025120 4 1 1 3 5 2 3 3 5 19 1 182 3 19 4 949
72420025120 4 2 1 6 5 1 6 6 2 19 1 182 3 19 4 949
72420025120 4 3 1 7 3 1 5 4 3 19 1 182 3 19 4 949
72420025120 4 4 1 5 5 1 6 4 3 19 1 182 3 19 4 949
72420025120 4 6 2 6 4 1 7 2 3 19 1 182 3 19 4 949
72420025120 5 1 1 4 5 2 3 5 5 19 1 182 3 19 4 949
72420025120 5 3 1 3 7 1 3 4 3 19 1 182 3 19 4 949
72420025120 5 4 1 5 3 1 5 5 3 19 1 182 3 19 4 949
72420025120 5 5 1 6 6 1 5 5 3 19 1 182 3 19 4 949
72420025120 5 6 1 NA NA 1 2 3 6 19 1 182 3 19 4 949

Create observation variable

To investigate dynamic research questions, for instance - does the amount consumed in a consumption episode predict the amount that will be consumed in the following consumption episode? - its generally necessary that you have a variable in your dataset that indicates how data rows relate to each other. That is, you will need a variable that tells a relevant R function which data pertains to the first, second, third, fourth, etc. questionnaire sent to a particular participant. The function will then use this information to ensure the data from the first questionnaire sent to, say, participant 14 will be used to predict the data from the second questionnaire sent to this participant, which will itself be used to predict data from the third questionnaire sent to this participant, etc.

ESM platforms/applications often do not record or export such a variable to your dataset. Therefore, it may be necessary to create one using other dataset variables. Below, I use the ‘mutate’ function from the dplyr package to create the variable ‘Obs’ (i.e., ‘Observation’). I use the variables ‘DAY’ and ‘SIG’ (i.e., signal) to determine how each row relates to each other (e.g., the 6th signal/questionnaire sent to a participant on day 4 of the ESM phase, is the 27th questionnaire sent to that participant overall).

datX7 <- mutate(datX6, Obs = case_when((DAY == 1 & SIG == 1) ~ 1,
                                               (DAY == 1 & SIG == 2) ~ 2,
                                               (DAY == 1 & SIG == 3) ~ 3,
                                               (DAY == 1 & SIG == 4) ~ 4,
                                               (DAY == 1 & SIG == 5) ~ 5,
                                               (DAY == 1 & SIG == 6) ~ 6,
                                               (DAY == 1 & SIG == 7) ~ 7,
                                               (DAY == 2 & SIG == 1) ~ 8,
                                               (DAY == 2 & SIG == 2) ~ 9,
                                               (DAY == 2 & SIG == 3) ~ 10,
                                               (DAY == 2 & SIG == 4) ~ 11,
                                               (DAY == 2 & SIG == 5) ~ 12,
                                               (DAY == 2 & SIG == 6) ~ 13,
                                               (DAY == 2 & SIG == 7) ~ 14,
                                               (DAY == 3 & SIG == 1) ~ 15,
                                               (DAY == 3 & SIG == 2) ~ 16,
                                               (DAY == 3 & SIG == 3) ~ 17,
                                               (DAY == 3 & SIG == 4) ~ 18,
                                               (DAY == 3 & SIG == 5) ~ 19,
                                               (DAY == 3 & SIG == 6) ~ 20,
                                               (DAY == 3 & SIG == 7) ~ 21,
                                               (DAY == 4 & SIG == 1) ~ 22,
                                               (DAY == 4 & SIG == 2) ~ 23,
                                               (DAY == 4 & SIG == 3) ~ 24,
                                               (DAY == 4 & SIG == 4) ~ 25,
                                               (DAY == 4 & SIG == 5) ~ 26,
                                               (DAY == 4 & SIG == 6) ~ 27,
                                               (DAY == 4 & SIG == 7) ~ 28,
                                               (DAY == 5 & SIG == 1) ~ 29,
                                               (DAY == 5 & SIG == 2) ~ 30,
                                               (DAY == 5 & SIG == 3) ~ 31,
                                               (DAY == 5 & SIG == 4) ~ 32,
                                               (DAY == 5 & SIG == 5) ~ 33,
                                               (DAY == 5 & SIG == 6) ~ 34,
                                               (DAY == 5 & SIG == 7) ~ 35,
                                               (DAY == 6 & SIG == 1) ~ 36,
                                               (DAY == 6 & SIG == 2) ~ 37,
                                               (DAY == 6 & SIG == 3) ~ 38,
                                               (DAY == 6 & SIG == 4) ~ 39,
                                               (DAY == 6 & SIG == 5) ~ 40,
                                               (DAY == 6 & SIG == 6) ~ 41,
                                               (DAY == 6 & SIG == 7) ~ 42,
                                               (DAY == 7 & SIG == 1) ~ 43,
                                               (DAY == 7 & SIG == 2) ~ 44,
                                               (DAY == 7 & SIG == 3) ~ 45,
                                               (DAY == 7 & SIG == 4) ~ 46,
                                               (DAY == 7 & SIG == 5) ~ 47,
                                               (DAY == 7 & SIG == 6) ~ 48,
                                               (DAY == 7 & SIG == 7) ~ 49, TRUE ~ 0))
PID DAY SIG NumCEpi Satisfied MoreGrati NumCBeh ActEnj ActCon ExpEnj Age Gender HeightT1 HeightT1Sure Nationality EmployStat DurationIntake Obs
72320021024 1 1 3 4 5 2 4 4 2 22 1 180 4 19 4 612 1
72320021024 1 3 2 5 5 2 3 6 6 22 1 180 4 19 4 612 3
72320021024 1 4 1 5 6 1 5 5 5 22 1 180 4 19 4 612 4
72320021024 1 5 1 3 6 1 3 5 5 22 1 180 4 19 4 612 5
72320021024 1 7 2 2 1 1 2 4 3 22 1 180 4 19 4 612 7
72320021024 2 1 1 5 2 2 5 5 5 22 1 180 4 19 4 612 8
72320021024 2 2 1 3 4 1 3 6 2 22 1 180 4 19 4 612 9
72320021024 2 3 1 3 6 1 3 2 2 22 1 180 4 19 4 612 10
72320021024 2 4 1 4 5 2 5 4 6 22 1 180 4 19 4 612 11
72320021024 2 5 1 4 5 1 2 4 3 22 1 180 4 19 4 612 12
72320021024 2 6 1 3 6 2 3 4 4 22 1 180 4 19 4 612 13
72320021024 2 7 1 3 5 2 4 4 3 22 1 180 4 19 4 612 14
72320021024 3 2 1 5 5 2 5 4 5 22 1 180 4 19 4 612 16
72320021024 3 3 1 4 5 2 4 5 5 22 1 180 4 19 4 612 17
72320021024 3 5 1 4 5 1 4 3 3 22 1 180 4 19 4 612 19
72320021024 3 6 1 5 4 1 5 6 5 22 1 180 4 19 4 612 20
72320021024 3 7 1 5 5 2 4 5 4 22 1 180 4 19 4 612 21
72320021024 4 1 1 5 3 1 5 4 5 22 1 180 4 19 4 612 22
72320021024 4 2 1 4 5 1 3 6 5 22 1 180 4 19 4 612 23
72320021024 4 3 1 3 3 1 4 4 3 22 1 180 4 19 4 612 24
72320021024 4 4 1 4 5 1 4 3 4 22 1 180 4 19 4 612 25
72320021024 4 5 1 4 6 1 4 5 3 22 1 180 4 19 4 612 26
72320021024 4 6 1 6 5 2 6 5 5 22 1 180 4 19 4 612 27
72320021024 4 7 1 5 5 1 4 4 3 22 1 180 4 19 4 612 28
72320021024 5 1 1 4 5 1 3 4 3 22 1 180 4 19 4 612 29
72320021024 5 2 1 5 3 2 5 4 5 22 1 180 4 19 4 612 30
72320021024 5 3 1 4 6 2 4 2 5 22 1 180 4 19 4 612 31
72320021024 5 4 1 3 5 1 3 4 3 22 1 180 4 19 4 612 32
72320021024 5 5 1 5 3 1 4 4 6 22 1 180 4 19 4 612 33
72320021024 5 6 1 6 3 2 6 5 5 22 1 180 4 19 4 612 34
72320021024 5 7 1 6 2 1 2 6 5 22 1 180 4 19 4 612 35
72320021024 6 1 1 4 5 1 3 4 4 22 1 180 4 19 4 612 36
72320021024 6 2 1 5 5 1 5 2 5 22 1 180 4 19 4 612 37
72320021024 6 3 1 4 6 2 4 3 4 22 1 180 4 19 4 612 38
72320021024 6 4 1 3 6 1 4 5 3 22 1 180 4 19 4 612 39
72320021024 6 5 1 5 5 1 6 5 5 22 1 180 4 19 4 612 40
72320021024 6 6 1 5 5 1 5 4 5 22 1 180 4 19 4 612 41
72320021024 6 7 1 6 5 2 6 3 5 22 1 180 4 19 4 612 42
72320021024 7 1 1 5 3 2 5 5 5 22 1 180 4 19 4 612 43
72320021024 7 2 1 3 5 1 5 3 5 22 1 180 4 19 4 612 44
72320021024 7 3 1 5 5 1 5 5 5 22 1 180 4 19 4 612 45
72320021024 7 5 1 6 2 1 6 6 6 22 1 180 4 19 4 612 47
72320021024 7 6 1 5 4 1 6 6 6 22 1 180 4 19 4 612 48
72320021024 7 7 1 5 2 2 5 6 4 22 1 180 4 19 4 612 49
72320022623 1 1 1 6 2 3 5 4 5 21 2 168 4 19 4 785 1
72320022623 1 3 1 7 1 2 7 4 7 21 2 168 4 19 4 785 3
72320022623 1 4 1 4 2 2 4 2 4 21 2 168 4 19 4 785 4
72320022623 1 5 1 3 5 2 3 2 4 21 2 168 4 19 4 785 5
72320022623 1 6 2 5 6 1 5 4 6 21 2 168 4 19 4 785 6
72320022623 2 1 1 5 3 3 5 7 7 21 2 168 4 19 4 785 8
72320022623 2 2 1 5 6 3 5 5 6 21 2 168 4 19 4 785 9
72320022623 2 3 1 3 5 1 3 3 3 21 2 168 4 19 4 785 10
72320022623 2 4 3 2 6 1 3 3 3 21 2 168 4 19 4 785 11
72320022623 2 5 1 3 6 1 4 3 4 21 2 168 4 19 4 785 12
72320022623 2 6 1 3 5 1 3 4 5 21 2 168 4 19 4 785 13
72320022623 2 7 2 6 5 1 5 4 5 21 2 168 4 19 4 785 14
72320022623 3 1 1 5 5 2 5 5 6 21 2 168 4 19 4 785 15
72320022623 3 2 1 3 5 2 5 4 5 21 2 168 4 19 4 785 16
72320022623 3 3 1 6 3 1 6 4 5 21 2 168 4 19 4 785 17
72320022623 3 5 4 4 5 1 4 5 4 21 2 168 4 19 4 785 19
72320022623 3 6 1 6 3 1 6 4 6 21 2 168 4 19 4 785 20
72320022623 3 7 2 4 5 1 3 4 3 21 2 168 4 19 4 785 21
72320022623 4 1 3 6 3 1 5 4 5 21 2 168 4 19 4 785 22
72320022623 4 2 1 5 6 1 5 5 6 21 2 168 4 19 4 785 23
72320022623 4 3 2 3 7 1 4 4 5 21 2 168 4 19 4 785 24
72320022623 4 4 1 3 5 1 4 4 4 21 2 168 4 19 4 785 25
72320022623 4 5 2 5 3 1 6 4 6 21 2 168 4 19 4 785 26
72320022623 4 6 1 4 5 1 4 5 4 21 2 168 4 19 4 785 27
72320022623 4 7 2 6 5 1 5 4 4 21 2 168 4 19 4 785 28
72320022623 5 1 2 5 5 2 5 5 6 21 2 168 4 19 4 785 29

Centering variables

Multilevel modelling is frequently the analytic approach of choice for ESM data, because it ensures standard errors are appropriately adjusted due to numerous datapoints coming from the same participant (i.e., the data is clustered, thus violating the assumptions of single-level regression, Hox, Moerbeek, and Van de Schoot 2017). However, to conduct multilevel modelling it is often necessary to center your predictors. If you don’t, regression coefficients may be difficult to interpret (Enders and Tofighi 2007).

To begin you should create person-mean versions of your predictors. So lets say my predictor variable from the above table is ‘MoreGrati’, I can use the below code to make a person-mean version of it. It is important for this function to work that there are no missing values for the variable ‘PID’. Thus, I first use code to remove rows where ‘PID’ is missing. The table that follows shows this worked, and that the mean need for more gratification of participant ‘72320021024’ is 4.48.

datX7 <- data.frame(datX7[!is.na(datX7$PID),])
datX7$MeanMoreGrati <- calc.mean(MoreGrati, PID, data=datX7, expand=TRUE)
PID DAY SIG NumCEpi Satisfied MoreGrati NumCBeh ActEnj ActCon ExpEnj Age Gender HeightT1 HeightT1Sure Nationality EmployStat DurationIntake Obs MeanMoreGrati
72320021024 1 1 3 4 5 2 4 4 2 22 1 180 4 19 4 612 1 4.477273
72320021024 1 3 2 5 5 2 3 6 6 22 1 180 4 19 4 612 3 4.477273
72320021024 1 4 1 5 6 1 5 5 5 22 1 180 4 19 4 612 4 4.477273
72320021024 1 5 1 3 6 1 3 5 5 22 1 180 4 19 4 612 5 4.477273
72320021024 1 7 2 2 1 1 2 4 3 22 1 180 4 19 4 612 7 4.477273
72320021024 2 1 1 5 2 2 5 5 5 22 1 180 4 19 4 612 8 4.477273
72320021024 2 2 1 3 4 1 3 6 2 22 1 180 4 19 4 612 9 4.477273
72320021024 2 3 1 3 6 1 3 2 2 22 1 180 4 19 4 612 10 4.477273
72320021024 2 4 1 4 5 2 5 4 6 22 1 180 4 19 4 612 11 4.477273
72320021024 2 5 1 4 5 1 2 4 3 22 1 180 4 19 4 612 12 4.477273
72320021024 2 6 1 3 6 2 3 4 4 22 1 180 4 19 4 612 13 4.477273
72320021024 2 7 1 3 5 2 4 4 3 22 1 180 4 19 4 612 14 4.477273
72320021024 3 2 1 5 5 2 5 4 5 22 1 180 4 19 4 612 16 4.477273
72320021024 3 3 1 4 5 2 4 5 5 22 1 180 4 19 4 612 17 4.477273
72320021024 3 5 1 4 5 1 4 3 3 22 1 180 4 19 4 612 19 4.477273
72320021024 3 6 1 5 4 1 5 6 5 22 1 180 4 19 4 612 20 4.477273
72320021024 3 7 1 5 5 2 4 5 4 22 1 180 4 19 4 612 21 4.477273
72320021024 4 1 1 5 3 1 5 4 5 22 1 180 4 19 4 612 22 4.477273
72320021024 4 2 1 4 5 1 3 6 5 22 1 180 4 19 4 612 23 4.477273
72320021024 4 3 1 3 3 1 4 4 3 22 1 180 4 19 4 612 24 4.477273
72320021024 4 4 1 4 5 1 4 3 4 22 1 180 4 19 4 612 25 4.477273
72320021024 4 5 1 4 6 1 4 5 3 22 1 180 4 19 4 612 26 4.477273
72320021024 4 6 1 6 5 2 6 5 5 22 1 180 4 19 4 612 27 4.477273
72320021024 4 7 1 5 5 1 4 4 3 22 1 180 4 19 4 612 28 4.477273
72320021024 5 1 1 4 5 1 3 4 3 22 1 180 4 19 4 612 29 4.477273
72320021024 5 2 1 5 3 2 5 4 5 22 1 180 4 19 4 612 30 4.477273
72320021024 5 3 1 4 6 2 4 2 5 22 1 180 4 19 4 612 31 4.477273
72320021024 5 4 1 3 5 1 3 4 3 22 1 180 4 19 4 612 32 4.477273
72320021024 5 5 1 5 3 1 4 4 6 22 1 180 4 19 4 612 33 4.477273
72320021024 5 6 1 6 3 2 6 5 5 22 1 180 4 19 4 612 34 4.477273
72320021024 5 7 1 6 2 1 2 6 5 22 1 180 4 19 4 612 35 4.477273
72320021024 6 1 1 4 5 1 3 4 4 22 1 180 4 19 4 612 36 4.477273
72320021024 6 2 1 5 5 1 5 2 5 22 1 180 4 19 4 612 37 4.477273
72320021024 6 3 1 4 6 2 4 3 4 22 1 180 4 19 4 612 38 4.477273
72320021024 6 4 1 3 6 1 4 5 3 22 1 180 4 19 4 612 39 4.477273
72320021024 6 5 1 5 5 1 6 5 5 22 1 180 4 19 4 612 40 4.477273
72320021024 6 6 1 5 5 1 5 4 5 22 1 180 4 19 4 612 41 4.477273
72320021024 6 7 1 6 5 2 6 3 5 22 1 180 4 19 4 612 42 4.477273
72320021024 7 1 1 5 3 2 5 5 5 22 1 180 4 19 4 612 43 4.477273
72320021024 7 2 1 3 5 1 5 3 5 22 1 180 4 19 4 612 44 4.477273
72320021024 7 3 1 5 5 1 5 5 5 22 1 180 4 19 4 612 45 4.477273
72320021024 7 5 1 6 2 1 6 6 6 22 1 180 4 19 4 612 47 4.477273
72320021024 7 6 1 5 4 1 6 6 6 22 1 180 4 19 4 612 48 4.477273
72320021024 7 7 1 5 2 2 5 6 4 22 1 180 4 19 4 612 49 4.477273
72320022623 1 1 1 6 2 3 5 4 5 21 2 168 4 19 4 785 1 4.348837
72320022623 1 3 1 7 1 2 7 4 7 21 2 168 4 19 4 785 3 4.348837
72320022623 1 4 1 4 2 2 4 2 4 21 2 168 4 19 4 785 4 4.348837
72320022623 1 5 1 3 5 2 3 2 4 21 2 168 4 19 4 785 5 4.348837
72320022623 1 6 2 5 6 1 5 4 6 21 2 168 4 19 4 785 6 4.348837
72320022623 2 1 1 5 3 3 5 7 7 21 2 168 4 19 4 785 8 4.348837
72320022623 2 2 1 5 6 3 5 5 6 21 2 168 4 19 4 785 9 4.348837
72320022623 2 3 1 3 5 1 3 3 3 21 2 168 4 19 4 785 10 4.348837
72320022623 2 4 3 2 6 1 3 3 3 21 2 168 4 19 4 785 11 4.348837
72320022623 2 5 1 3 6 1 4 3 4 21 2 168 4 19 4 785 12 4.348837
72320022623 2 6 1 3 5 1 3 4 5 21 2 168 4 19 4 785 13 4.348837
72320022623 2 7 2 6 5 1 5 4 5 21 2 168 4 19 4 785 14 4.348837
72320022623 3 1 1 5 5 2 5 5 6 21 2 168 4 19 4 785 15 4.348837
72320022623 3 2 1 3 5 2 5 4 5 21 2 168 4 19 4 785 16 4.348837
72320022623 3 3 1 6 3 1 6 4 5 21 2 168 4 19 4 785 17 4.348837
72320022623 3 5 4 4 5 1 4 5 4 21 2 168 4 19 4 785 19 4.348837
72320022623 3 6 1 6 3 1 6 4 6 21 2 168 4 19 4 785 20 4.348837
72320022623 3 7 2 4 5 1 3 4 3 21 2 168 4 19 4 785 21 4.348837
72320022623 4 1 3 6 3 1 5 4 5 21 2 168 4 19 4 785 22 4.348837
72320022623 4 2 1 5 6 1 5 5 6 21 2 168 4 19 4 785 23 4.348837
72320022623 4 3 2 3 7 1 4 4 5 21 2 168 4 19 4 785 24 4.348837
72320022623 4 4 1 3 5 1 4 4 4 21 2 168 4 19 4 785 25 4.348837
72320022623 4 5 2 5 3 1 6 4 6 21 2 168 4 19 4 785 26 4.348837
72320022623 4 6 1 4 5 1 4 5 4 21 2 168 4 19 4 785 27 4.348837
72320022623 4 7 2 6 5 1 5 4 4 21 2 168 4 19 4 785 28 4.348837
72320022623 5 1 2 5 5 2 5 5 6 21 2 168 4 19 4 785 29 4.348837


The next step is grand-mean centering this newly created variable (other centering options are possible and sometimes preferable, Enders and Tofighi 2007). Doing so will enable us to interpret how our predictor relates to our outcome variable in relation to the mean of the predictor (e.g,. a 1-point increase in ‘MoreGrati’ from the average level of ‘MoreGrati’ in the sample, associates with a X-point increase in the outcome variable). I grand-mean center this variable below.

datX7$MeanMoreGrati <- center(datX7$MeanMoreGrati, type = "CGM", value = NULL, as.na = NULL,check = TRUE)

Now, rather than the variable ‘MeanMoreGrati’ being 4.48 for participant ‘72320021024’, it is 1.69. This variable can now be interpreted as the participant having an average need for more gratification that is 1.69 points above the sample-mean value.

PID DAY SIG NumCEpi Satisfied MoreGrati NumCBeh ActEnj ActCon ExpEnj Age Gender HeightT1 HeightT1Sure Nationality EmployStat DurationIntake Obs MeanMoreGrati
72320021024 1 1 3 4 5 2 4 4 2 22 1 180 4 19 4 612 1 1.688454
72320021024 1 3 2 5 5 2 3 6 6 22 1 180 4 19 4 612 3 1.688454
72320021024 1 4 1 5 6 1 5 5 5 22 1 180 4 19 4 612 4 1.688454
72320021024 1 5 1 3 6 1 3 5 5 22 1 180 4 19 4 612 5 1.688454
72320021024 1 7 2 2 1 1 2 4 3 22 1 180 4 19 4 612 7 1.688454
72320021024 2 1 1 5 2 2 5 5 5 22 1 180 4 19 4 612 8 1.688454
72320021024 2 2 1 3 4 1 3 6 2 22 1 180 4 19 4 612 9 1.688454
72320021024 2 3 1 3 6 1 3 2 2 22 1 180 4 19 4 612 10 1.688454
72320021024 2 4 1 4 5 2 5 4 6 22 1 180 4 19 4 612 11 1.688454
72320021024 2 5 1 4 5 1 2 4 3 22 1 180 4 19 4 612 12 1.688454
72320021024 2 6 1 3 6 2 3 4 4 22 1 180 4 19 4 612 13 1.688454
72320021024 2 7 1 3 5 2 4 4 3 22 1 180 4 19 4 612 14 1.688454
72320021024 3 2 1 5 5 2 5 4 5 22 1 180 4 19 4 612 16 1.688454
72320021024 3 3 1 4 5 2 4 5 5 22 1 180 4 19 4 612 17 1.688454
72320021024 3 5 1 4 5 1 4 3 3 22 1 180 4 19 4 612 19 1.688454
72320021024 3 6 1 5 4 1 5 6 5 22 1 180 4 19 4 612 20 1.688454
72320021024 3 7 1 5 5 2 4 5 4 22 1 180 4 19 4 612 21 1.688454
72320021024 4 1 1 5 3 1 5 4 5 22 1 180 4 19 4 612 22 1.688454
72320021024 4 2 1 4 5 1 3 6 5 22 1 180 4 19 4 612 23 1.688454
72320021024 4 3 1 3 3 1 4 4 3 22 1 180 4 19 4 612 24 1.688454
72320021024 4 4 1 4 5 1 4 3 4 22 1 180 4 19 4 612 25 1.688454
72320021024 4 5 1 4 6 1 4 5 3 22 1 180 4 19 4 612 26 1.688454
72320021024 4 6 1 6 5 2 6 5 5 22 1 180 4 19 4 612 27 1.688454
72320021024 4 7 1 5 5 1 4 4 3 22 1 180 4 19 4 612 28 1.688454
72320021024 5 1 1 4 5 1 3 4 3 22 1 180 4 19 4 612 29 1.688454
72320021024 5 2 1 5 3 2 5 4 5 22 1 180 4 19 4 612 30 1.688454
72320021024 5 3 1 4 6 2 4 2 5 22 1 180 4 19 4 612 31 1.688454
72320021024 5 4 1 3 5 1 3 4 3 22 1 180 4 19 4 612 32 1.688454
72320021024 5 5 1 5 3 1 4 4 6 22 1 180 4 19 4 612 33 1.688454
72320021024 5 6 1 6 3 2 6 5 5 22 1 180 4 19 4 612 34 1.688454
72320021024 5 7 1 6 2 1 2 6 5 22 1 180 4 19 4 612 35 1.688454
72320021024 6 1 1 4 5 1 3 4 4 22 1 180 4 19 4 612 36 1.688454
72320021024 6 2 1 5 5 1 5 2 5 22 1 180 4 19 4 612 37 1.688454
72320021024 6 3 1 4 6 2 4 3 4 22 1 180 4 19 4 612 38 1.688454
72320021024 6 4 1 3 6 1 4 5 3 22 1 180 4 19 4 612 39 1.688454
72320021024 6 5 1 5 5 1 6 5 5 22 1 180 4 19 4 612 40 1.688454
72320021024 6 6 1 5 5 1 5 4 5 22 1 180 4 19 4 612 41 1.688454
72320021024 6 7 1 6 5 2 6 3 5 22 1 180 4 19 4 612 42 1.688454
72320021024 7 1 1 5 3 2 5 5 5 22 1 180 4 19 4 612 43 1.688454
72320021024 7 2 1 3 5 1 5 3 5 22 1 180 4 19 4 612 44 1.688454
72320021024 7 3 1 5 5 1 5 5 5 22 1 180 4 19 4 612 45 1.688454
72320021024 7 5 1 6 2 1 6 6 6 22 1 180 4 19 4 612 47 1.688454
72320021024 7 6 1 5 4 1 6 6 6 22 1 180 4 19 4 612 48 1.688454
72320021024 7 7 1 5 2 2 5 6 4 22 1 180 4 19 4 612 49 1.688454
72320022623 1 1 1 6 2 3 5 4 5 21 2 168 4 19 4 785 1 1.560018
72320022623 1 3 1 7 1 2 7 4 7 21 2 168 4 19 4 785 3 1.560018
72320022623 1 4 1 4 2 2 4 2 4 21 2 168 4 19 4 785 4 1.560018
72320022623 1 5 1 3 5 2 3 2 4 21 2 168 4 19 4 785 5 1.560018
72320022623 1 6 2 5 6 1 5 4 6 21 2 168 4 19 4 785 6 1.560018
72320022623 2 1 1 5 3 3 5 7 7 21 2 168 4 19 4 785 8 1.560018
72320022623 2 2 1 5 6 3 5 5 6 21 2 168 4 19 4 785 9 1.560018
72320022623 2 3 1 3 5 1 3 3 3 21 2 168 4 19 4 785 10 1.560018
72320022623 2 4 3 2 6 1 3 3 3 21 2 168 4 19 4 785 11 1.560018
72320022623 2 5 1 3 6 1 4 3 4 21 2 168 4 19 4 785 12 1.560018
72320022623 2 6 1 3 5 1 3 4 5 21 2 168 4 19 4 785 13 1.560018
72320022623 2 7 2 6 5 1 5 4 5 21 2 168 4 19 4 785 14 1.560018
72320022623 3 1 1 5 5 2 5 5 6 21 2 168 4 19 4 785 15 1.560018
72320022623 3 2 1 3 5 2 5 4 5 21 2 168 4 19 4 785 16 1.560018
72320022623 3 3 1 6 3 1 6 4 5 21 2 168 4 19 4 785 17 1.560018
72320022623 3 5 4 4 5 1 4 5 4 21 2 168 4 19 4 785 19 1.560018
72320022623 3 6 1 6 3 1 6 4 6 21 2 168 4 19 4 785 20 1.560018
72320022623 3 7 2 4 5 1 3 4 3 21 2 168 4 19 4 785 21 1.560018
72320022623 4 1 3 6 3 1 5 4 5 21 2 168 4 19 4 785 22 1.560018
72320022623 4 2 1 5 6 1 5 5 6 21 2 168 4 19 4 785 23 1.560018
72320022623 4 3 2 3 7 1 4 4 5 21 2 168 4 19 4 785 24 1.560018
72320022623 4 4 1 3 5 1 4 4 4 21 2 168 4 19 4 785 25 1.560018
72320022623 4 5 2 5 3 1 6 4 6 21 2 168 4 19 4 785 26 1.560018
72320022623 4 6 1 4 5 1 4 5 4 21 2 168 4 19 4 785 27 1.560018
72320022623 4 7 2 6 5 1 5 4 4 21 2 168 4 19 4 785 28 1.560018
72320022623 5 1 2 5 5 2 5 5 6 21 2 168 4 19 4 785 29 1.560018


Finally, you’ll often want to create a person-mean centered version of your original predictor variables. Modelling this within-person version of your variables will ensure you are modelling only within-person effects (which will generally be your focus - e.g., need for more gratification in that instance predicting the amount consumed in the next consumption episode), and not simultaneously within- and between-person variance (which can ultimately render your coefficients uninterpretable, Enders and Tofighi 2007). I person-mean center the variable ‘MoreGrati’ using the below code.

datX7$CentMoreGrati <- calc.mcent(MoreGrati, PID, data=datX7)

So now we have a within-person centered version of our ‘MoreGrati’ variable. For observation 1 for participant ‘72320021024’ on row X, we can see this value is ‘0.523’. This can be interpreted as the participants need for more gratification being 0.523 points above their own mean value for this variable.

PID DAY SIG NumCEpi Satisfied MoreGrati NumCBeh ActEnj ActCon ExpEnj Age Gender HeightT1 HeightT1Sure Nationality EmployStat DurationIntake Obs MeanMoreGrati CentMoreGrati
72320021024 1 1 3 4 5 2 4 4 2 22 1 180 4 19 4 612 1 1.688454 0.5227273
72320021024 1 3 2 5 5 2 3 6 6 22 1 180 4 19 4 612 3 1.688454 0.5227273
72320021024 1 4 1 5 6 1 5 5 5 22 1 180 4 19 4 612 4 1.688454 1.5227273
72320021024 1 5 1 3 6 1 3 5 5 22 1 180 4 19 4 612 5 1.688454 1.5227273
72320021024 1 7 2 2 1 1 2 4 3 22 1 180 4 19 4 612 7 1.688454 -3.4772727
72320021024 2 1 1 5 2 2 5 5 5 22 1 180 4 19 4 612 8 1.688454 -2.4772727
72320021024 2 2 1 3 4 1 3 6 2 22 1 180 4 19 4 612 9 1.688454 -0.4772727
72320021024 2 3 1 3 6 1 3 2 2 22 1 180 4 19 4 612 10 1.688454 1.5227273
72320021024 2 4 1 4 5 2 5 4 6 22 1 180 4 19 4 612 11 1.688454 0.5227273
72320021024 2 5 1 4 5 1 2 4 3 22 1 180 4 19 4 612 12 1.688454 0.5227273
72320021024 2 6 1 3 6 2 3 4 4 22 1 180 4 19 4 612 13 1.688454 1.5227273
72320021024 2 7 1 3 5 2 4 4 3 22 1 180 4 19 4 612 14 1.688454 0.5227273
72320021024 3 2 1 5 5 2 5 4 5 22 1 180 4 19 4 612 16 1.688454 0.5227273
72320021024 3 3 1 4 5 2 4 5 5 22 1 180 4 19 4 612 17 1.688454 0.5227273
72320021024 3 5 1 4 5 1 4 3 3 22 1 180 4 19 4 612 19 1.688454 0.5227273
72320021024 3 6 1 5 4 1 5 6 5 22 1 180 4 19 4 612 20 1.688454 -0.4772727
72320021024 3 7 1 5 5 2 4 5 4 22 1 180 4 19 4 612 21 1.688454 0.5227273
72320021024 4 1 1 5 3 1 5 4 5 22 1 180 4 19 4 612 22 1.688454 -1.4772727
72320021024 4 2 1 4 5 1 3 6 5 22 1 180 4 19 4 612 23 1.688454 0.5227273
72320021024 4 3 1 3 3 1 4 4 3 22 1 180 4 19 4 612 24 1.688454 -1.4772727
72320021024 4 4 1 4 5 1 4 3 4 22 1 180 4 19 4 612 25 1.688454 0.5227273
72320021024 4 5 1 4 6 1 4 5 3 22 1 180 4 19 4 612 26 1.688454 1.5227273
72320021024 4 6 1 6 5 2 6 5 5 22 1 180 4 19 4 612 27 1.688454 0.5227273
72320021024 4 7 1 5 5 1 4 4 3 22 1 180 4 19 4 612 28 1.688454 0.5227273
72320021024 5 1 1 4 5 1 3 4 3 22 1 180 4 19 4 612 29 1.688454 0.5227273
72320021024 5 2 1 5 3 2 5 4 5 22 1 180 4 19 4 612 30 1.688454 -1.4772727
72320021024 5 3 1 4 6 2 4 2 5 22 1 180 4 19 4 612 31 1.688454 1.5227273
72320021024 5 4 1 3 5 1 3 4 3 22 1 180 4 19 4 612 32 1.688454 0.5227273
72320021024 5 5 1 5 3 1 4 4 6 22 1 180 4 19 4 612 33 1.688454 -1.4772727
72320021024 5 6 1 6 3 2 6 5 5 22 1 180 4 19 4 612 34 1.688454 -1.4772727
72320021024 5 7 1 6 2 1 2 6 5 22 1 180 4 19 4 612 35 1.688454 -2.4772727
72320021024 6 1 1 4 5 1 3 4 4 22 1 180 4 19 4 612 36 1.688454 0.5227273
72320021024 6 2 1 5 5 1 5 2 5 22 1 180 4 19 4 612 37 1.688454 0.5227273
72320021024 6 3 1 4 6 2 4 3 4 22 1 180 4 19 4 612 38 1.688454 1.5227273
72320021024 6 4 1 3 6 1 4 5 3 22 1 180 4 19 4 612 39 1.688454 1.5227273
72320021024 6 5 1 5 5 1 6 5 5 22 1 180 4 19 4 612 40 1.688454 0.5227273
72320021024 6 6 1 5 5 1 5 4 5 22 1 180 4 19 4 612 41 1.688454 0.5227273
72320021024 6 7 1 6 5 2 6 3 5 22 1 180 4 19 4 612 42 1.688454 0.5227273
72320021024 7 1 1 5 3 2 5 5 5 22 1 180 4 19 4 612 43 1.688454 -1.4772727
72320021024 7 2 1 3 5 1 5 3 5 22 1 180 4 19 4 612 44 1.688454 0.5227273
72320021024 7 3 1 5 5 1 5 5 5 22 1 180 4 19 4 612 45 1.688454 0.5227273
72320021024 7 5 1 6 2 1 6 6 6 22 1 180 4 19 4 612 47 1.688454 -2.4772727
72320021024 7 6 1 5 4 1 6 6 6 22 1 180 4 19 4 612 48 1.688454 -0.4772727
72320021024 7 7 1 5 2 2 5 6 4 22 1 180 4 19 4 612 49 1.688454 -2.4772727
72320022623 1 1 1 6 2 3 5 4 5 21 2 168 4 19 4 785 1 1.560018 -2.3488372
72320022623 1 3 1 7 1 2 7 4 7 21 2 168 4 19 4 785 3 1.560018 -3.3488372
72320022623 1 4 1 4 2 2 4 2 4 21 2 168 4 19 4 785 4 1.560018 -2.3488372
72320022623 1 5 1 3 5 2 3 2 4 21 2 168 4 19 4 785 5 1.560018 0.6511628
72320022623 1 6 2 5 6 1 5 4 6 21 2 168 4 19 4 785 6 1.560018 1.6511628
72320022623 2 1 1 5 3 3 5 7 7 21 2 168 4 19 4 785 8 1.560018 -1.3488372
72320022623 2 2 1 5 6 3 5 5 6 21 2 168 4 19 4 785 9 1.560018 1.6511628
72320022623 2 3 1 3 5 1 3 3 3 21 2 168 4 19 4 785 10 1.560018 0.6511628
72320022623 2 4 3 2 6 1 3 3 3 21 2 168 4 19 4 785 11 1.560018 1.6511628
72320022623 2 5 1 3 6 1 4 3 4 21 2 168 4 19 4 785 12 1.560018 1.6511628
72320022623 2 6 1 3 5 1 3 4 5 21 2 168 4 19 4 785 13 1.560018 0.6511628
72320022623 2 7 2 6 5 1 5 4 5 21 2 168 4 19 4 785 14 1.560018 0.6511628
72320022623 3 1 1 5 5 2 5 5 6 21 2 168 4 19 4 785 15 1.560018 0.6511628
72320022623 3 2 1 3 5 2 5 4 5 21 2 168 4 19 4 785 16 1.560018 0.6511628
72320022623 3 3 1 6 3 1 6 4 5 21 2 168 4 19 4 785 17 1.560018 -1.3488372
72320022623 3 5 4 4 5 1 4 5 4 21 2 168 4 19 4 785 19 1.560018 0.6511628
72320022623 3 6 1 6 3 1 6 4 6 21 2 168 4 19 4 785 20 1.560018 -1.3488372
72320022623 3 7 2 4 5 1 3 4 3 21 2 168 4 19 4 785 21 1.560018 0.6511628
72320022623 4 1 3 6 3 1 5 4 5 21 2 168 4 19 4 785 22 1.560018 -1.3488372
72320022623 4 2 1 5 6 1 5 5 6 21 2 168 4 19 4 785 23 1.560018 1.6511628
72320022623 4 3 2 3 7 1 4 4 5 21 2 168 4 19 4 785 24 1.560018 2.6511628
72320022623 4 4 1 3 5 1 4 4 4 21 2 168 4 19 4 785 25 1.560018 0.6511628
72320022623 4 5 2 5 3 1 6 4 6 21 2 168 4 19 4 785 26 1.560018 -1.3488372
72320022623 4 6 1 4 5 1 4 5 4 21 2 168 4 19 4 785 27 1.560018 0.6511628
72320022623 4 7 2 6 5 1 5 4 4 21 2 168 4 19 4 785 28 1.560018 0.6511628
72320022623 5 1 2 5 5 2 5 5 6 21 2 168 4 19 4 785 29 1.560018 0.6511628

Lagging variables

To enable dynamic test of effects (e.g., does the level of food overconsumption in a consumption episode predict/cause how much a participant will consume in the following consumption episode?) you will need to copy (i.e., lag) the value of your predictor variables to the next measurement instance (e.g., values of predictor variable from the second questionnaire sent to a participant being copied to the row containing the values of the outcome variable from the third questionnaire sent to that participant).

You can do this using various functions and packages, however, perhaps the easiest approach is to use the ‘lagvar’ function of the esmpack package. I do this below for the newly created ‘CentMoreGrati’ variable. By including ‘PID’,‘Obs’, and ‘DAY’ in this function, I ensure the predictor variable value will not be lagged if it comes from the final questionnaire of the day (DAY), if there is a gap between observations (Obs), and if the next row does not belong to the same participant (PID).

datX7$LagCentMoreGrati <- lagvar(CentMoreGrati, PID, Obs, DAY, data=datX7)

The below table shows this function has worked effectively.

PID DAY SIG NumCEpi Satisfied MoreGrati NumCBeh ActEnj ActCon ExpEnj Age Gender HeightT1 HeightT1Sure Nationality EmployStat DurationIntake Obs MeanMoreGrati CentMoreGrati LagCentMoreGrati
72320021024 1 1 3 4 5 2 4 4 2 22 1 180 4 19 4 612 1 1.688454 0.5227273 NA
72320021024 1 3 2 5 5 2 3 6 6 22 1 180 4 19 4 612 3 1.688454 0.5227273 NA
72320021024 1 4 1 5 6 1 5 5 5 22 1 180 4 19 4 612 4 1.688454 1.5227273 0.5227273
72320021024 1 5 1 3 6 1 3 5 5 22 1 180 4 19 4 612 5 1.688454 1.5227273 1.5227273
72320021024 1 7 2 2 1 1 2 4 3 22 1 180 4 19 4 612 7 1.688454 -3.4772727 NA
72320021024 2 1 1 5 2 2 5 5 5 22 1 180 4 19 4 612 8 1.688454 -2.4772727 NA
72320021024 2 2 1 3 4 1 3 6 2 22 1 180 4 19 4 612 9 1.688454 -0.4772727 -2.4772727
72320021024 2 3 1 3 6 1 3 2 2 22 1 180 4 19 4 612 10 1.688454 1.5227273 -0.4772727
72320021024 2 4 1 4 5 2 5 4 6 22 1 180 4 19 4 612 11 1.688454 0.5227273 1.5227273
72320021024 2 5 1 4 5 1 2 4 3 22 1 180 4 19 4 612 12 1.688454 0.5227273 0.5227273
72320021024 2 6 1 3 6 2 3 4 4 22 1 180 4 19 4 612 13 1.688454 1.5227273 0.5227273
72320021024 2 7 1 3 5 2 4 4 3 22 1 180 4 19 4 612 14 1.688454 0.5227273 1.5227273
72320021024 3 2 1 5 5 2 5 4 5 22 1 180 4 19 4 612 16 1.688454 0.5227273 NA
72320021024 3 3 1 4 5 2 4 5 5 22 1 180 4 19 4 612 17 1.688454 0.5227273 0.5227273
72320021024 3 5 1 4 5 1 4 3 3 22 1 180 4 19 4 612 19 1.688454 0.5227273 NA
72320021024 3 6 1 5 4 1 5 6 5 22 1 180 4 19 4 612 20 1.688454 -0.4772727 0.5227273
72320021024 3 7 1 5 5 2 4 5 4 22 1 180 4 19 4 612 21 1.688454 0.5227273 -0.4772727
72320021024 4 1 1 5 3 1 5 4 5 22 1 180 4 19 4 612 22 1.688454 -1.4772727 NA
72320021024 4 2 1 4 5 1 3 6 5 22 1 180 4 19 4 612 23 1.688454 0.5227273 -1.4772727
72320021024 4 3 1 3 3 1 4 4 3 22 1 180 4 19 4 612 24 1.688454 -1.4772727 0.5227273
72320021024 4 4 1 4 5 1 4 3 4 22 1 180 4 19 4 612 25 1.688454 0.5227273 -1.4772727
72320021024 4 5 1 4 6 1 4 5 3 22 1 180 4 19 4 612 26 1.688454 1.5227273 0.5227273
72320021024 4 6 1 6 5 2 6 5 5 22 1 180 4 19 4 612 27 1.688454 0.5227273 1.5227273
72320021024 4 7 1 5 5 1 4 4 3 22 1 180 4 19 4 612 28 1.688454 0.5227273 0.5227273
72320021024 5 1 1 4 5 1 3 4 3 22 1 180 4 19 4 612 29 1.688454 0.5227273 NA
72320021024 5 2 1 5 3 2 5 4 5 22 1 180 4 19 4 612 30 1.688454 -1.4772727 0.5227273
72320021024 5 3 1 4 6 2 4 2 5 22 1 180 4 19 4 612 31 1.688454 1.5227273 -1.4772727
72320021024 5 4 1 3 5 1 3 4 3 22 1 180 4 19 4 612 32 1.688454 0.5227273 1.5227273
72320021024 5 5 1 5 3 1 4 4 6 22 1 180 4 19 4 612 33 1.688454 -1.4772727 0.5227273
72320021024 5 6 1 6 3 2 6 5 5 22 1 180 4 19 4 612 34 1.688454 -1.4772727 -1.4772727
72320021024 5 7 1 6 2 1 2 6 5 22 1 180 4 19 4 612 35 1.688454 -2.4772727 -1.4772727
72320021024 6 1 1 4 5 1 3 4 4 22 1 180 4 19 4 612 36 1.688454 0.5227273 NA
72320021024 6 2 1 5 5 1 5 2 5 22 1 180 4 19 4 612 37 1.688454 0.5227273 0.5227273
72320021024 6 3 1 4 6 2 4 3 4 22 1 180 4 19 4 612 38 1.688454 1.5227273 0.5227273
72320021024 6 4 1 3 6 1 4 5 3 22 1 180 4 19 4 612 39 1.688454 1.5227273 1.5227273
72320021024 6 5 1 5 5 1 6 5 5 22 1 180 4 19 4 612 40 1.688454 0.5227273 1.5227273
72320021024 6 6 1 5 5 1 5 4 5 22 1 180 4 19 4 612 41 1.688454 0.5227273 0.5227273
72320021024 6 7 1 6 5 2 6 3 5 22 1 180 4 19 4 612 42 1.688454 0.5227273 0.5227273
72320021024 7 1 1 5 3 2 5 5 5 22 1 180 4 19 4 612 43 1.688454 -1.4772727 NA
72320021024 7 2 1 3 5 1 5 3 5 22 1 180 4 19 4 612 44 1.688454 0.5227273 -1.4772727
72320021024 7 3 1 5 5 1 5 5 5 22 1 180 4 19 4 612 45 1.688454 0.5227273 0.5227273
72320021024 7 5 1 6 2 1 6 6 6 22 1 180 4 19 4 612 47 1.688454 -2.4772727 NA
72320021024 7 6 1 5 4 1 6 6 6 22 1 180 4 19 4 612 48 1.688454 -0.4772727 -2.4772727
72320021024 7 7 1 5 2 2 5 6 4 22 1 180 4 19 4 612 49 1.688454 -2.4772727 -0.4772727
72320022623 1 1 1 6 2 3 5 4 5 21 2 168 4 19 4 785 1 1.560018 -2.3488372 NA
72320022623 1 3 1 7 1 2 7 4 7 21 2 168 4 19 4 785 3 1.560018 -3.3488372 NA
72320022623 1 4 1 4 2 2 4 2 4 21 2 168 4 19 4 785 4 1.560018 -2.3488372 -3.3488372
72320022623 1 5 1 3 5 2 3 2 4 21 2 168 4 19 4 785 5 1.560018 0.6511628 -2.3488372
72320022623 1 6 2 5 6 1 5 4 6 21 2 168 4 19 4 785 6 1.560018 1.6511628 0.6511628
72320022623 2 1 1 5 3 3 5 7 7 21 2 168 4 19 4 785 8 1.560018 -1.3488372 NA
72320022623 2 2 1 5 6 3 5 5 6 21 2 168 4 19 4 785 9 1.560018 1.6511628 -1.3488372
72320022623 2 3 1 3 5 1 3 3 3 21 2 168 4 19 4 785 10 1.560018 0.6511628 1.6511628
72320022623 2 4 3 2 6 1 3 3 3 21 2 168 4 19 4 785 11 1.560018 1.6511628 0.6511628
72320022623 2 5 1 3 6 1 4 3 4 21 2 168 4 19 4 785 12 1.560018 1.6511628 1.6511628
72320022623 2 6 1 3 5 1 3 4 5 21 2 168 4 19 4 785 13 1.560018 0.6511628 1.6511628
72320022623 2 7 2 6 5 1 5 4 5 21 2 168 4 19 4 785 14 1.560018 0.6511628 0.6511628
72320022623 3 1 1 5 5 2 5 5 6 21 2 168 4 19 4 785 15 1.560018 0.6511628 NA
72320022623 3 2 1 3 5 2 5 4 5 21 2 168 4 19 4 785 16 1.560018 0.6511628 0.6511628
72320022623 3 3 1 6 3 1 6 4 5 21 2 168 4 19 4 785 17 1.560018 -1.3488372 0.6511628
72320022623 3 5 4 4 5 1 4 5 4 21 2 168 4 19 4 785 19 1.560018 0.6511628 NA
72320022623 3 6 1 6 3 1 6 4 6 21 2 168 4 19 4 785 20 1.560018 -1.3488372 0.6511628
72320022623 3 7 2 4 5 1 3 4 3 21 2 168 4 19 4 785 21 1.560018 0.6511628 -1.3488372
72320022623 4 1 3 6 3 1 5 4 5 21 2 168 4 19 4 785 22 1.560018 -1.3488372 NA
72320022623 4 2 1 5 6 1 5 5 6 21 2 168 4 19 4 785 23 1.560018 1.6511628 -1.3488372
72320022623 4 3 2 3 7 1 4 4 5 21 2 168 4 19 4 785 24 1.560018 2.6511628 1.6511628
72320022623 4 4 1 3 5 1 4 4 4 21 2 168 4 19 4 785 25 1.560018 0.6511628 2.6511628
72320022623 4 5 2 5 3 1 6 4 6 21 2 168 4 19 4 785 26 1.560018 -1.3488372 0.6511628
72320022623 4 6 1 4 5 1 4 5 4 21 2 168 4 19 4 785 27 1.560018 0.6511628 -1.3488372
72320022623 4 7 2 6 5 1 5 4 4 21 2 168 4 19 4 785 28 1.560018 0.6511628 0.6511628
72320022623 5 1 2 5 5 2 5 5 6 21 2 168 4 19 4 785 29 1.560018 0.6511628 NA

References

Conner, Tamlin S., Howard Tennen, William Fleeson, and Lisa Feldman Barrett. 2009. “Experience Sampling Methods: A Modern Idiographic Approach to Personality Research.” Social and Personality Psychology Compass 3 (3): 292–313.
Enders, Craig K., and Davood Tofighi. 2007. “Centering Predictor Variables in Cross-Sectional Multilevel Models: A New Look at an Old Issue.” Psychological Methods 12 (2): 121.
Hox, Joop J., Mirjam Moerbeek, and Rens Van de Schoot. 2017. Multilevel Analysis: Techniques and Applications. Routledge.
Stephen Murphy
Stephen Murphy
Postdoctoral Research Fellow in Psychology

My research interests include self-regulation, motivation, meta-science, and data science.