Skip to contents

Calculates SOPg_calculated = (WATERg + PROCNTg + FAT_g_combined + CHOAVLg + FIBTGg_combined + ALCg +ASHg). Column names are case sensitive and an error is returned if not found. Non-numeric columns will be converted if possible in function to be numeric.

Usage

SOPg_calculator(
  df,
  WATERg_column = "WATERg",
  PROCNTg_column = "PROCNTg",
  FAT_g_combined_column = "FAT_g_combined",
  CHOAVLg_column = "CHOAVLg",
  FIBTGg_combined_column = "FIBTGg_combined",
  ALCg_column = "ALCg",
  ASHg_column = "ASHg",
  comment = TRUE,
  comment_col = "comments",
  OutsideBoundsReplacement = "none",
  LowerBound = 93,
  UpperBound = 107,
  OutsideBoundsDF = FALSE
)

Arguments

df

Required - the data.frame the data is currently stored in.

WATERg_column

Required - default: 'WATERg' - The name of the column containing Water/moisture content in grams per 100g of Edible Portion (EP).

PROCNTg_column

Required - default: 'PROCNTg' - Protein in grams per 100g of Edible Portion (EP), as reported in the original FCT and assumed to be calculated from nitrogen (NTg) content.

FAT_g_combined_column

Required - default: 'FAT_g_combined' - Fat content, unknown method of calculation, in grams per 100g of Edible Portion (EP).

CHOAVLg_column

Required - default: 'CHOAVLg' - Available carbohydrates calculated by difference, in grams per 100g of Edible Portion (EP).

FIBTGg_combined_column

Required - default: 'FIBTGg_combined' - Fibre content from combined Tagnames, with preference of Total dietary fibre by AOAC Prosky method, expressed in grams per 100g of Edible Portion (EP).

ALCg_column

Required - default: 'ALCg' - Alcohol in grams per 100g of Edible Portion (EP).

ASHg_column

Required - default: 'ASHg' - Ashes in grams per 100g of Edible Portion (EP).

comment

Required - default: TRUE - TRUE or FALSE. If comment is set to TRUE (as it is by default), when the function is run a comment describing the source of the SOPg_calculated column is added to the comment_col. If no comment_col is selected, and comment = TRUE, one is created, called comments.

comment_col

Optional - default: 'comments' - A potential input variable; the column which contains the metadata comments for the food item in question. Not required if the comment parameter is set to FALSE.

OutsideBoundsReplacement

Required - default: 'none' - Options are 'round', NA, 'remove', or 'none'. Choose what happens to values that are outside of the bounds. The ranges are set to FAO standards: 93-107 is considered acceptable. This parameter decides what happens to those values less than 93, or over 107. If set to round, then outside of bound values are set to the closest acceptable value (e.g. 90 -> 93, 111 -> 107. If set to NA, they are replaced with NA. if set to 'remove', then those rows are removed (including NA results). if set to 'none', then they are left as the out of bound values.

LowerBound

Required - default: 93 - Integer value. Sets the lower boundary for acceptable SOPg_calculated values, and therefore determines the values affected by OutsideBoundsReplacement and OutsideBoundsDF. FAO standards list 93 as the lower boundary for acceptable values, and 95 as the lower boundary for preferred values.

UpperBound

Required - default: 107 - Integer value. Sets the upper boundary for acceptable SOPg_calculated values, and therefore determines the values affected by OutsideBoundsReplacement and OutsideBoundsDF. FAO standards list 107 as the upper boundary for acceptable values, and 105 as the upper boundary for preferred values.

OutsideBoundsDF

Required - default: FALSE - TRUE or FALSE. If set to TRUE, Then the output switches from being a copy of the input df with the the SOPg_calculated column to a subset of that dataframe only showing SOPg_calculated values that are out of bounds, for manual inspection.

Value

Original data.frame with a new SOPg_calculated column, and (depending on the options selected) an additional comment/comments column and comment.

Examples

# Two example data.frames have been prepared to illustrate the
# SOPg_calculator. The first is a dataset of fictional food values to
# illustrate the various options in the function, and the second is a dataset
# with non-standard column names, to show how to specify columns.

# This is the first data.frame - before the SOPg_calculator has been used on it.
breakfast_df <- breakfast_df[,c("food_code", "food_name", "WATERg",
"PROCNTg", "FAT_g_combined", "CHOAVLg", "FIBTGg_combined", "ALCg", "ASHg",
"comments")]
breakfast_df
#>    food_code      food_name WATERg PROCNTg FAT_g_combined CHOAVLg
#> 1      F0001          Bacon     10      15             21      10
#> 2      F0002          Beans     15      10             12       1
#> 3      F0003          Toast     20      20             NA      24
#> 4      F0004       Mushroom     25      15             16      46
#> 5      F0005           Eggs     30      21             11      20
#> 6      F0006         Tomato     35      28             33       2
#> 7      F0007        Sausage     40      10             13      24
#> 8      F0008         Butter     NA      27             16      22
#> 9      F0009    Brown Sauce     NA      NA             NA      NA
#> 10     F0010 Tomato Ketchup     NA      NA             NA      NA
#>    FIBTGg_combined ALCg ASHg                       comments
#> 1               12   12   12                               
#> 2                3   43    3 These are imaginary food items
#> 3                8    8   28                           <NA>
#> 4               15   15   15 With imaginary nutrient values
#> 5                6    6    6                               
#> 6                2    2    2                     And blanks
#> 7                9    9    9                           <NA>
#> 8               13   13   13      To test different outputs
#> 9               NA   NA   NA                               
#> 10              NA   NA   NA                  And scenarios
#
#
# First, an example of the standard usecase - calculate the SOPg_calculated
# value, without modifying out of bounds values.
nothing_results <- SOPg_calculator(breakfast_df, OutsideBoundsReplacement = "none")
#> ---------------------------
#> 
#> 7 SOPg_calculated values calculated to be Out of Bounds (less than 93 or higher than 107). Largest distance from 100: 47. Number of NA's: 2. Please rerun the function with OutsideBoundsDF = TRUE if you wish to inspect these values.
#> 
#> Out of Bounds values left untouched, as per user input.
#> 
#> ---------------------------
#
nothing_results
#>    food_code      food_name WATERg PROCNTg FAT_g_combined CHOAVLg
#> 1      F0001          Bacon     10      15             21      10
#> 2      F0002          Beans     15      10             12       1
#> 3      F0003          Toast     20      20             NA      24
#> 4      F0004       Mushroom     25      15             16      46
#> 5      F0005           Eggs     30      21             11      20
#> 6      F0006         Tomato     35      28             33       2
#> 7      F0007        Sausage     40      10             13      24
#> 8      F0008         Butter     NA      27             16      22
#> 9      F0009    Brown Sauce     NA      NA             NA      NA
#> 10     F0010 Tomato Ketchup     NA      NA             NA      NA
#>    FIBTGg_combined ALCg ASHg
#> 1               12   12   12
#> 2                3   43    3
#> 3                8    8   28
#> 4               15   15   15
#> 5                6    6    6
#> 6                2    2    2
#> 7                9    9    9
#> 8               13   13   13
#> 9               NA   NA   NA
#> 10              NA   NA   NA
#>                                                                               comments
#> 1                                  SOPg_calculated calculated from adding constituents
#> 2  These are imaginary food items; SOPg_calculated calculated from adding constituents
#> 3                                  SOPg_calculated calculated from adding constituents
#> 4  With imaginary nutrient values; SOPg_calculated calculated from adding constituents
#> 5                                  SOPg_calculated calculated from adding constituents
#> 6                      And blanks; SOPg_calculated calculated from adding constituents
#> 7                                  SOPg_calculated calculated from adding constituents
#> 8       To test different outputs; SOPg_calculated calculated from adding constituents
#> 9                                  SOPg_calculated calculated from adding constituents
#> 10                  And scenarios; SOPg_calculated calculated from adding constituents
#>    SOPg_calculated
#> 1               92
#> 2               87
#> 3              108
#> 4              147
#> 5              100
#> 6              104
#> 7              114
#> 8              104
#> 9               NA
#> 10              NA
# See the changes - the addition of the SOPg_calculated column, and the
# additions to the comments column.
#
#
# The second example shows the results when the Replacement option is set to NA
NA_results <- SOPg_calculator(breakfast_df, OutsideBoundsReplacement = NA)
#> ---------------------------
#> 
#> 7 SOPg_calculated values calculated to be Out of Bounds (less than 93 or higher than 107). Largest distance from 100: 47. Number of NA's: 2. Please rerun the function with OutsideBoundsDF = TRUE if you wish to inspect these values.
#> 
#> Out of Bounds values set to NA, as per user input.
#> 
#> ---------------------------
#
NA_results
#>    food_code      food_name WATERg PROCNTg FAT_g_combined CHOAVLg
#> 1      F0001          Bacon     10      15             21      10
#> 2      F0002          Beans     15      10             12       1
#> 3      F0003          Toast     20      20             NA      24
#> 4      F0004       Mushroom     25      15             16      46
#> 5      F0005           Eggs     30      21             11      20
#> 6      F0006         Tomato     35      28             33       2
#> 7      F0007        Sausage     40      10             13      24
#> 8      F0008         Butter     NA      27             16      22
#> 9      F0009    Brown Sauce     NA      NA             NA      NA
#> 10     F0010 Tomato Ketchup     NA      NA             NA      NA
#>    FIBTGg_combined ALCg ASHg
#> 1               12   12   12
#> 2                3   43    3
#> 3                8    8   28
#> 4               15   15   15
#> 5                6    6    6
#> 6                2    2    2
#> 7                9    9    9
#> 8               13   13   13
#> 9               NA   NA   NA
#> 10              NA   NA   NA
#>                                                                                                                   comments
#> 1                                   SOPg_calculated calculated from adding constituents - Original value of 92 reset to NA
#> 2   These are imaginary food items; SOPg_calculated calculated from adding constituents - Original value of 87 reset to NA
#> 3                                  SOPg_calculated calculated from adding constituents - Original value of 108 reset to NA
#> 4  With imaginary nutrient values; SOPg_calculated calculated from adding constituents - Original value of 147 reset to NA
#> 5                                                                      SOPg_calculated calculated from adding constituents
#> 6                                                          And blanks; SOPg_calculated calculated from adding constituents
#> 7                                  SOPg_calculated calculated from adding constituents - Original value of 114 reset to NA
#> 8                                           To test different outputs; SOPg_calculated calculated from adding constituents
#> 9                                                                                                                         
#> 10                                                                                                           And scenarios
#>    SOPg_calculated
#> 1               NA
#> 2               NA
#> 3               NA
#> 4               NA
#> 5              100
#> 6              104
#> 7               NA
#> 8              104
#> 9               NA
#> 10              NA
# Check the SOP column and comments column again - see how values outside of
# bounds have been replaced with NA, and a note of this change logged in the
# comments column.
#
#
# The third example shows the results when the Replacement option is set to 'remove'
remove_results <- SOPg_calculator(breakfast_df, OutsideBoundsReplacement = "remove")
#> ---------------------------
#> 
#> 7 SOPg_calculated values calculated to be Out of Bounds (less than 93 or higher than 107). Largest distance from 100: 47. Number of NA's: 2. Please rerun the function with OutsideBoundsDF = TRUE if you wish to inspect these values.
#> 
#> Out of Bounds value rows removed, as per user input.
#> 
#> ---------------------------
#
remove_results
#>   food_code food_name WATERg PROCNTg FAT_g_combined CHOAVLg FIBTGg_combined
#> 5     F0005      Eggs     30      21             11      20               6
#> 6     F0006    Tomato     35      28             33       2               2
#> 8     F0008    Butter     NA      27             16      22              13
#>   ALCg ASHg
#> 5    6    6
#> 6    2    2
#> 8   13   13
#>                                                                         comments
#> 5                            SOPg_calculated calculated from adding constituents
#> 6                And blanks; SOPg_calculated calculated from adding constituents
#> 8 To test different outputs; SOPg_calculated calculated from adding constituents
#>   SOPg_calculated
#> 5             100
#> 6             104
#> 8             104
# See how the out of bounds values have been removed.
#
#
# The fourth example is of the rounding results.
rounding_results <- SOPg_calculator(breakfast_df, OutsideBoundsReplacement = "round")
#> ---------------------------
#> 
#> 7 SOPg_calculated values calculated to be Out of Bounds (less than 93 or higher than 107). Largest distance from 100: 47. Number of NA's: 2. Please rerun the function with OutsideBoundsDF = TRUE if you wish to inspect these values.
#> 
#> Out of Bounds values set to closest acceptable value, as per user input.
#> 
#> ---------------------------
#
rounding_results
#>    food_code      food_name WATERg PROCNTg FAT_g_combined CHOAVLg
#> 1      F0001          Bacon     10      15             21      10
#> 2      F0002          Beans     15      10             12       1
#> 3      F0003          Toast     20      20             NA      24
#> 4      F0004       Mushroom     25      15             16      46
#> 5      F0005           Eggs     30      21             11      20
#> 6      F0006         Tomato     35      28             33       2
#> 7      F0007        Sausage     40      10             13      24
#> 8      F0008         Butter     NA      27             16      22
#> 9      F0009    Brown Sauce     NA      NA             NA      NA
#> 10     F0010 Tomato Ketchup     NA      NA             NA      NA
#>    FIBTGg_combined ALCg ASHg
#> 1               12   12   12
#> 2                3   43    3
#> 3                8    8   28
#> 4               15   15   15
#> 5                6    6    6
#> 6                2    2    2
#> 7                9    9    9
#> 8               13   13   13
#> 9               NA   NA   NA
#> 10              NA   NA   NA
#>                                                                                                                    comments
#> 1                                    SOPg_calculated calculated from adding constituents - Original value of 92 reset to 93
#> 2    These are imaginary food items; SOPg_calculated calculated from adding constituents - Original value of 87 reset to 93
#> 3                                  SOPg_calculated calculated from adding constituents - Original value of 108 reset to 107
#> 4  With imaginary nutrient values; SOPg_calculated calculated from adding constituents - Original value of 147 reset to 107
#> 5                                                                       SOPg_calculated calculated from adding constituents
#> 6                                                           And blanks; SOPg_calculated calculated from adding constituents
#> 7                                  SOPg_calculated calculated from adding constituents - Original value of 114 reset to 107
#> 8                                            To test different outputs; SOPg_calculated calculated from adding constituents
#> 9                                                                       SOPg_calculated calculated from adding constituents
#> 10                                                                                                            And scenarios
#>    SOPg_calculated
#> 1               93
#> 2               93
#> 3              107
#> 4              107
#> 5              100
#> 6              104
#> 7              107
#> 8              104
#> 9               NA
#> 10              NA
# Look at the SOP_combined values - and see how they've been capped to the bounds
# if they would have been out fo bounds, with a note of the change in the comments.
#
#
# The fifth example is of the out of bounds dataframe - an option useful for identifying
# and examining out of bounds results.
OoB_DF_results <- SOPg_calculator(breakfast_df, OutsideBoundsDF = TRUE)
#
OoB_DF_results
#>    food_code      food_name WATERg PROCNTg FAT_g_combined CHOAVLg
#> 1      F0001          Bacon     10      15             21      10
#> 2      F0002          Beans     15      10             12       1
#> 3      F0003          Toast     20      20             NA      24
#> 4      F0004       Mushroom     25      15             16      46
#> 7      F0007        Sausage     40      10             13      24
#> 9      F0009    Brown Sauce     NA      NA             NA      NA
#> 10     F0010 Tomato Ketchup     NA      NA             NA      NA
#>    FIBTGg_combined ALCg ASHg                       comments SOPg_calculated
#> 1               12   12   12                                             92
#> 2                3   43    3 These are imaginary food items              87
#> 3                8    8   28                           <NA>             108
#> 4               15   15   15 With imaginary nutrient values             147
#> 7                9    9    9                           <NA>             114
#> 9               NA   NA   NA                                             NA
#> 10              NA   NA   NA                  And scenarios              NA
# Only the out of bounds results are present, in their original form, for inspection.
#
#
# The sixth example is of the SOPg_calculator working on a dataframe with non-standard
# column names. It uses a modified example data frame, shown below.
breakfast_df_nonstandard <- breakfast_df_nonstandard[,c("food_code",
"food_name", "Water_values_g", "FAT_values_g_combined", "CHOAVL_values_g",
"PROCNT_values_g", "FIBTG_values_g_combined", "ALC_values_g", "ASH_values_g",
"comments_column")]
breakfast_df_nonstandard
#>    food_code      food_name Water_values_g FAT_values_g_combined
#> 1      F0001          Bacon             10                    21
#> 2      F0002          Beans             15                    12
#> 3      F0003          Toast             20                    NA
#> 4      F0004       Mushroom             25                    16
#> 5      F0005           Eggs             30                    11
#> 6      F0006         Tomato             35                    33
#> 7      F0007        Sausage             40                    13
#> 8      F0008         Butter             NA                    16
#> 9      F0009    Brown Sauce             NA                    NA
#> 10     F0010 Tomato Ketchup             NA                    NA
#>    CHOAVL_values_g PROCNT_values_g FIBTG_values_g_combined ALC_values_g
#> 1               10              15                      12           12
#> 2                1              10                       3           43
#> 3               24              20                       8            8
#> 4               46              15                      15           15
#> 5               20              21                       6            6
#> 6                2              28                       2            2
#> 7               24              10                       9            9
#> 8               22              27                      13           13
#> 9               NA              NA                      NA           NA
#> 10              NA              NA                      NA           NA
#>    ASH_values_g                comments_column
#> 1            12                               
#> 2             3 These are imaginary food items
#> 3            28                           <NA>
#> 4            15 With imaginary nutrient values
#> 5             6                               
#> 6             2                     And blanks
#> 7             9                           <NA>
#> 8            13      To test different outputs
#> 9            NA                               
#> 10           NA                  And scenarios
# Notice how the column names are different, and differ from the assumed names.
#
#
# Because of the different names, the column names for each input must be specified.
nothing_results_NonStandardInput <- SOPg_calculator(
breakfast_df_nonstandard,
WATERg_column = "Water_values_g",
PROCNTg_column = "PROCNT_values_g",
FAT_g_combined_column = "FAT_values_g_combined",
CHOAVLg_column = "CHOAVL_values_g",
FIBTGg_combined_column = "FIBTG_values_g_combined",
ALCg_column = "ALC_values_g",
ASHg_column = "ASH_values_g",
comment_col = "comments_column",
LowerBound = 97,
UpperBound = 103,
OutsideBoundsReplacement = "nothing")
#> ---------------------------
#> 
#> 9 SOPg_calculated values calculated to be Out of Bounds (less than 97 or higher than 103). Largest distance from 100: 47. Number of NA's: 2. Please rerun the function with OutsideBoundsDF = TRUE if you wish to inspect these values.
#> 
#> Out of Bounds values left untouched, as per user input.
#> 
#> ---------------------------
#
nothing_results_NonStandardInput
#>    food_code      food_name Water_values_g FAT_values_g_combined
#> 1      F0001          Bacon             10                    21
#> 2      F0002          Beans             15                    12
#> 3      F0003          Toast             20                    NA
#> 4      F0004       Mushroom             25                    16
#> 5      F0005           Eggs             30                    11
#> 6      F0006         Tomato             35                    33
#> 7      F0007        Sausage             40                    13
#> 8      F0008         Butter             NA                    16
#> 9      F0009    Brown Sauce             NA                    NA
#> 10     F0010 Tomato Ketchup             NA                    NA
#>    CHOAVL_values_g PROCNT_values_g FIBTG_values_g_combined ALC_values_g
#> 1               10              15                      12           12
#> 2                1              10                       3           43
#> 3               24              20                       8            8
#> 4               46              15                      15           15
#> 5               20              21                       6            6
#> 6                2              28                       2            2
#> 7               24              10                       9            9
#> 8               22              27                      13           13
#> 9               NA              NA                      NA           NA
#> 10              NA              NA                      NA           NA
#>    ASH_values_g
#> 1            12
#> 2             3
#> 3            28
#> 4            15
#> 5             6
#> 6             2
#> 7             9
#> 8            13
#> 9            NA
#> 10           NA
#>                                                                        comments_column
#> 1                                  SOPg_calculated calculated from adding constituents
#> 2  These are imaginary food items; SOPg_calculated calculated from adding constituents
#> 3                                  SOPg_calculated calculated from adding constituents
#> 4  With imaginary nutrient values; SOPg_calculated calculated from adding constituents
#> 5                                  SOPg_calculated calculated from adding constituents
#> 6                      And blanks; SOPg_calculated calculated from adding constituents
#> 7                                  SOPg_calculated calculated from adding constituents
#> 8       To test different outputs; SOPg_calculated calculated from adding constituents
#> 9                                  SOPg_calculated calculated from adding constituents
#> 10                  And scenarios; SOPg_calculated calculated from adding constituents
#>    SOPg_calculated
#> 1               92
#> 2               87
#> 3              108
#> 4              147
#> 5              100
#> 6              104
#> 7              114
#> 8              104
#> 9               NA
#> 10              NA
# The resulting SOPg_calculated column is the same as in the first example, despite the
# different names - although, due to the shift in the bounds, the warning message is not.