Introduction

Here we will calculate totals by occasion, user, and day.
This will be useful if a researcher wants to look at the sum of each eating occasion per participant. In contrast, the totals file we generated summed all the occasions of a certain day into one datapoint.


Load functions and packages

Name the path to DietR directory where input files are pulled.

main_wd <- "~/GitHub/DietR"

Load the necessary functions.

source("lib/specify_data_dir.R")
source("lib/calc_ASA24totals_by_occasion.R")

You can come back to the main directory by:

setwd(main_wd)

Specify the directory where the data is.

SpecifyDataDirectory(directory.name= "eg_data/VVKAJ/")


Generate sum of foods by occasion

Load your items data.

items_f_id_s_m_QCed <- read.delim("VVKAJ_Items_f_id_s_m_QCed.txt", sep="\t", header=T)

Generate sum of foods consumed by each user, by day, and by occasion.
Output will be saved as: Items_by_User_Occ.

SumByOccasion(items.data=items_f_id_s_m_QCed, User.Name='UserName', 
              Recall.No='RecallNo',   Occ.No='Occ_No')

Take a look at the first 10 rows of the first 6 columns.
Items are summed by occasion number for each participant’s RecallNo.

Items_by_User_Occ[1:10, 1:6]
##    User_Day_OccNo UserName RecallNo Occ_No FoodAmt     KCAL
## 1    VVKAJ101_1_1 VVKAJ101        1      1  510.60 626.2670
## 2    VVKAJ101_1_2 VVKAJ101        1      2  252.00   0.0000
## 3    VVKAJ101_1_3 VVKAJ101        1      3  249.75 366.0425
## 4    VVKAJ101_1_4 VVKAJ101        1      4  651.65 539.1745
## 5    VVKAJ101_1_5 VVKAJ101        1      5  738.50 783.4200
## 6    VVKAJ101_2_1 VVKAJ101        2      1  509.50 337.6500
## 7    VVKAJ101_2_2 VVKAJ101        2      2  415.00 177.4900
## 8    VVKAJ101_2_3 VVKAJ101        2      3  155.00 139.5000
## 9    VVKAJ101_2_4 VVKAJ101        2      4  304.15 258.9555
## 10   VVKAJ101_2_5 VVKAJ101        2      5  288.00   0.0000

The Items_by_User_Occ function adds occasion names (breakfast, snacks etc.) to items.data.
Output will be saved as: Sum_by_User_Day_Occ.

AddOccNames(items.data=items_f_id_s_m_QCed, User.Name='UserName', 
            Recall.No='RecallNo', Occ.No='Occ_No', Occ.Name='Occ_Name')

The output Sum_by_User_Day_Occ has the sum of foods consumed by each user, day, and occasion, with occasion names in words.

Take a look at the first 6 rows. Spelled-out occasion names corresponding to occasion name have been added at the end.

head(Sum_by_User_Day_Occ)
##   User_Day_OccNo UserName RecallNo Occ_No FoodAmt     KCAL     PROT     TFAT
## 1   VVKAJ101_1_1 VVKAJ101        1      1  510.60 626.2670  9.80520 12.29440
## 2   VVKAJ101_1_2 VVKAJ101        1      2  252.00   0.0000  0.00000  0.00000
## 3   VVKAJ101_1_3 VVKAJ101        1      3  249.75 366.0425 15.50113 29.88700
## 4   VVKAJ101_1_4 VVKAJ101        1      4  651.65 539.1745 26.30972  3.04656
## 5   VVKAJ101_1_5 VVKAJ101        1      5  738.50 783.4200 28.54115 37.74925
## 6   VVKAJ101_2_1 VVKAJ101        2      1  509.50 337.6500 11.35115  7.59715
##        CARB     MOIS ALC   CAFF THEO      SUGR     FIBE     CALC     IRON
## 1 127.39081 358.4300   0 26.784    0 62.543290 12.76850  52.0460 18.27292
## 2   0.00000 251.7480   0  0.000    0  0.000000  0.00000   7.5600  0.00000
## 3  10.44857 190.2593   0  0.000    0  4.002825  4.18575 149.2175  3.96320
## 4 112.74497 502.1475   0  0.000    0 68.438860 11.69125 289.2370  2.89618
## 5  87.21295 577.0344   0  0.000    0  9.223500 23.98000 391.7850  6.14970
## 6  56.95900 428.4122   0 76.800    0 27.929000  5.42250 262.2300  1.62895
##       MAGN    PHOS      POTA      SODI     ZINC      COPP     SELE      VC
## 1 107.0550 345.879  424.0100  308.8130 2.751890 0.4731170 43.95210  2.7230
## 2   2.5200   0.000    0.0000   10.0800 0.025200 0.0252000  0.00000  0.0000
## 3  95.6800 270.885  666.8725  491.0725 2.268950 0.2304475 31.64950 55.9875
## 4 195.6315 497.930 2013.7475  104.4170 2.550075 0.5184195 11.84765 70.7256
## 5 188.5000 672.760 1510.7700 1384.2800 4.180400 0.6888000 26.46600 79.5960
## 6 101.7700 334.690  669.5650  262.8800 1.837000 0.2415200 19.57600 81.2500
##         VB1       VB2      NIAC       VB6    FOLA     FA      FF    FDFE
## 1 0.7595200 0.8724350 10.508905 1.1006350 206.785 179.92  26.865 333.665
## 2 0.0000000 0.0000000  0.000000 0.0000000   0.000   0.00   0.000   0.000
## 3 0.2496500 0.6627575  1.249625 0.3829275 178.535   0.00 178.535 178.535
## 4 0.4174145 1.0105790  3.330598 1.6560110 182.521   0.00 182.521 182.521
## 5 0.4558950 0.4255350  3.638800 0.6902500 151.525   0.00 151.525 151.525
## 6 0.5046850 0.8888150  5.556840 0.6375500 121.865  85.07  36.795 181.555
##      VB12     VARA     RET     BCAR    ACAR   CRYP   LYCO       LZ     ATOC
## 1 3.01600 449.1700 447.200   32.780  0.0000  1.040   0.00  209.460 1.242400
## 2 0.00000   0.0000   0.000    0.000  0.0000  0.000   0.00    0.000 0.000000
## 3 1.01010 467.9200 134.310 3994.325  1.4400 18.070  13.35 7889.358 3.043125
## 4 0.84672 255.4215  30.240 2660.808 93.3965 21.168   0.00 5640.164 1.630535
## 5 0.29920  89.1550  61.435  316.635 11.8800 15.840   0.00  534.600 3.935250
## 6 2.23350 103.4300  82.350  225.435  7.0000 28.875 353.50  140.035 0.536400
##         VK   CHOLE      SFAT     S040     S060     S080     S100     S120
## 1  18.4420   0.000  1.092205 0.000000 0.000000 0.000000 0.000000 0.002080
## 2   0.0000   0.000  0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
## 3 363.2695 340.770  6.033577 0.000000 0.000000 0.002220 0.002220 0.002887
## 4 234.5353   9.072  1.338515 0.039312 0.027216 0.021168 0.032600 0.037984
## 5  58.4045  26.125 10.231760 0.271590 0.083435 0.094215 0.195525 0.143110
## 6   3.8620  18.300  3.928790 0.137250 0.137250 0.137250 0.137250 0.149775
##        S140     S160     S180       MFAT     M161       M181     M201    M221
## 1 0.0020800 0.834455 0.216755  6.1216350 0.005090  6.0750550 0.037440 0.00104
## 2 0.0000000 0.000000 0.000000  0.0000000 0.000000  0.0000000 0.000000 0.00000
## 3 0.0595570 4.189893 1.573217  8.7545500 0.308750  8.3356900 0.084170 0.00777
## 4 0.1298365 0.834111 0.190963  0.4705195 0.050816  0.4136555 0.000000 0.00000
## 5 1.0552850 5.589870 2.149125 12.7942100 0.374660 12.1712800 0.113355 0.00000
## 6 0.5504950 1.958100 0.698240  1.8666400 0.004975  1.8318600 0.000000 0.00000
##        PFAT       P182      P183 P184     P204    P205 P225    P226   VITD
## 1  3.498815  3.2158550 0.2829600    0 0.000000 0.00000    0 0.00000 1.9760
## 2  0.000000  0.0000000 0.0000000    0 0.000000 0.00000    0 0.00000 0.0000
## 3 12.797260 11.2482050 1.3573275    0 0.143857 0.00444    0 0.03441 1.9980
## 4  0.526016  0.3288785 0.1971375    0 0.000000 0.00000    0 0.00000 0.9072
## 5 12.136520 10.4085300 1.6884450    0 0.014080 0.00000    0 0.00000 0.0000
## 6  0.846860  0.6657100 0.1811500    0 0.000000 0.00000    0 0.00000 2.3790
##      CHOLN VITE_ADD B12_ADD  F_TOTAL F_CITMLB  F_OTHER F_JUICE  V_TOTAL V_DRKGR
## 1  36.0993        0   3.016 0.659950  0.65995 0.000000  0.0000 0.000000 0.00000
## 2   0.0000        0   0.000 0.000000  0.00000 0.000000  0.0000 0.000000 0.00000
## 3 293.9523        0   0.000 0.000000  0.00000 0.000000  0.0000 1.732125 0.99120
## 4 113.8396        0   0.000 3.337895  0.00000 3.035495  0.3024 0.635040 0.63504
## 5 128.6725        0   0.000 0.039600  0.00000 0.000000  0.0396 1.289200 0.03960
## 6  47.7865        0   1.410 0.630000  0.30625 0.323750  0.0000 0.000000 0.00000
##   V_REDOR_TOTAL V_REDOR_TOMATO V_REDOR_OTHER V_STARCHY_TOTAL V_STARCHY_POTATO
## 1             0              0             0               0                0
## 2             0              0             0               0                0
## 3             0              0             0               0                0
## 4             0              0             0               0                0
## 5             0              0             0               0                0
## 6             0              0             0               0                0
##   V_STARCHY_OTHER  V_OTHER V_LEGUMES G_TOTAL G_WHOLE G_REFINED PF_TOTAL
## 1               0 0.000000   0.00000  2.7456  2.6520    0.0936   0.7384
## 2               0 0.000000   0.00000  0.0000  0.0000    0.0000   0.0000
## 3               0 0.740925   0.00000  0.0000  0.0000    0.0000   2.3733
## 4               0 0.000000   0.00000  0.0000  0.0000    0.0000   0.0000
## 5               0 1.249600   1.25675  2.6928  0.0000    2.6928   0.0000
## 6               0 0.000000   0.00000  1.1797  0.5969    0.5828   0.0000
##   PF_MPS_TOTAL PF_MEAT PF_CUREDMEAT PF_ORGAN PF_POULT PF_SEAFD_HI PF_SEAFD_LOW
## 1            0       0            0        0        0           0            0
## 2            0       0            0        0        0           0            0
## 3            0       0            0        0        0           0            0
## 4            0       0            0        0        0           0            0
## 5            0       0            0        0        0           0            0
## 6            0       0            0        0        0           0            0
##   PF_EGGS PF_SOY PF_NUTSDS PF_LEGUMES D_TOTAL  D_MILK D_YOGURT D_CHEESE    OILS
## 1  0.0000      0    0.7384    0.00000 0.00000 0.00000        0   0.0000  6.0736
## 2  0.0000      0    0.0000    0.00000 0.00000 0.00000        0   0.0000  0.0000
## 3  1.8093      0    0.5640    0.00000 0.00000 0.00000        0   0.0000 18.2068
## 4  0.0000      0    0.0000    0.00000 0.78624 0.21168        0   0.0000  0.0000
## 5  0.0000      0    0.0000    4.93515 0.55440 0.00000        0   0.5544 22.4840
## 6  0.0000      0    0.0000    0.00000 0.75030 0.75030        0   0.0000  0.0000
##   SOLID_FATS ADD_SUGARS A_DRINKS Occ_Name Occ_In_Words
## 1    0.00000   12.53787        0        1    Breakfast
## 2    0.00000    0.00000        0        7 Just a drink
## 3    4.85070    0.00000        0        3        Lunch
## 4    0.78624    0.00000        0        6        Snack
## 5    8.94520    0.00000        0        4       Dinner
## 6    4.83120    2.27480        0        1    Breakfast



Save Sum_by_User_Day_Occ as a txt file.

write.table(Sum_by_User_Day_Occ, 'VVKAJ_Sum_by_User_Day_Occ.txt', sep="\t", row.names=F, quote=F)



Come back to the main directory before you start running another script.

  setwd(main_wd)