Skip to contents

This function calculates potential values for Niacin, and then combines the most appropriate ones for each item into a new value. Inputs must cover at least two of the following combinations: NIAmg_column, NIAEQmg_column + NIATRPmg_column, NIAEQmg_column + TRPmg_column. This is because the function can then find Niacin values in multiple ways, and select the most appropriate.

The priority for use is 1st: Niacin, 2nd: Total Niacin Equivalents - (Tryptophan/60), 3rd: Total Niacin Equivalents - Niacin Equivalents from Tryptophan.

Usage

NIAmg_calc_combiner(
  df,
  NIAEQmg_column = "NIAEQmg",
  NIAmg_column = "NIAmg",
  TRPmg_column = "TRPmg",
  NIATRPmg_column = "NIATRPmg",
  comment = TRUE,
  comment_col = "comments"
)

Arguments

df

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

NIAEQmg_column

Required - default: 'NIAEQmg' - The name of the column containing Niacin equivalents, total (preformed Niacin as well as Niacin equivalents from Tryptophan) in mg per 100g of Edible Portion (EP). The only required input as its impossible to get 2 or more ways of calculating NIAmg_combined without it, which is required for the function to work.

NIAmg_column

Optional - default: 'NIAmg' - The name of the column containing Niacin (preformed) in mg per 100g of Edible Portion (EP). If unavailable, set input to NA.

TRPmg_column

Optional - default: 'TRPmg' - Tryptophan, in mg per 100g of Edible Portion (EP). If unavailable, set input to NA.

NIATRPmg_column

Optional - default: 'NIATRPmg' - The name of the column containing Niacin equivalents from Tryptophan, in mg per 100g of Edible Portion (EP). If unavailable, set input to NA.

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 calculation used to find the NIAmg_combined value is added to the comment_col. If no comment_col is selected, and comment = TRUE, one is created.

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 comment is set to FALSE. If comment is set to TRUE, and the comment_col input is not the name of a column found in the df, the function will create a column with the name of the comment_col input to store comments in.

Value

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

Examples

# We will go through three examples of the NIAmg_calc_combiner, two using
# standard names, and another with non-standard names.
breakfast_df <- breakfast_df[,c("food_code", "food_name", "NIAmg", "TRPmg",
"NIAEQmg", "NIATRPmg", "comments")]
breakfast_df
#>    food_code      food_name NIAmg TRPmg NIAEQmg NIATRPmg
#> 1      F0001          Bacon 172.0 126.0    85.3     2.10
#> 2      F0002          Beans    NA 142.5    49.2       NA
#> 3      F0003          Toast  24.4 142.7    86.4     2.38
#> 4      F0004       Mushroom    NA 167.0    23.2     2.80
#> 5      F0005           Eggs   8.1    NA    30.5       NA
#> 6      F0006         Tomato 134.6  76.3    83.3     1.27
#> 7      F0007        Sausage  10.2  98.6    16.6     1.64
#> 8      F0008         Butter 187.9  41.4    84.5     0.69
#> 9      F0009    Brown Sauce  92.0 172.0    17.7     2.87
#> 10     F0010 Tomato Ketchup    NA    NA      NA       NA
#>                          comments
#> 1                                
#> 2  These are imaginary food items
#> 3                            <NA>
#> 4  With imaginary nutrient values
#> 5                                
#> 6                      And blanks
#> 7                            <NA>
#> 8       To test different outputs
#> 9                                
#> 10                  And scenarios

# This is the first data.frame; you can see it has the food item information,
# the required columns for calculation, and a comments column. Everything
# needed to run the NIAmg_calc_combiner

NIA_results <- NIAmg_calc_combiner(breakfast_df)
#> ---------------------------
#> 
#> Breakdown of values used:
#> 
#> NIAmg_combined equal to NIAEQmg - (TRPmg/60) 
#>                                            2 
#>                NIAmg_combined equal to NIAmg 
#>                                            7 
#>   No suitable value for NIAmg_combined found 
#>                                            1 
#> 
#> ---------------------------

NIA_results
#>    food_code      food_name NIAmg TRPmg NIAEQmg NIATRPmg
#> 1      F0001          Bacon 172.0 126.0    85.3     2.10
#> 2      F0002          Beans    NA 142.5    49.2       NA
#> 3      F0003          Toast  24.4 142.7    86.4     2.38
#> 4      F0004       Mushroom    NA 167.0    23.2     2.80
#> 5      F0005           Eggs   8.1    NA    30.5       NA
#> 6      F0006         Tomato 134.6  76.3    83.3     1.27
#> 7      F0007        Sausage  10.2  98.6    16.6     1.64
#> 8      F0008         Butter 187.9  41.4    84.5     0.69
#> 9      F0009    Brown Sauce  92.0 172.0    17.7     2.87
#> 10     F0010 Tomato Ketchup    NA    NA      NA       NA
#>                                                                        comments
#> 1                                                 NIAmg_combined equal to NIAmg
#> 2  These are imaginary food items; NIAmg_combined equal to NIAEQmg - (TRPmg/60)
#> 3                                                 NIAmg_combined equal to NIAmg
#> 4  With imaginary nutrient values; NIAmg_combined equal to NIAEQmg - (TRPmg/60)
#> 5                                                 NIAmg_combined equal to NIAmg
#> 6                                     And blanks; NIAmg_combined equal to NIAmg
#> 7                                                 NIAmg_combined equal to NIAmg
#> 8                      To test different outputs; NIAmg_combined equal to NIAmg
#> 9                                                 NIAmg_combined equal to NIAmg
#> 10                    And scenarios; No suitable value for NIAmg_combined found
#>    NIAmg_combined
#> 1       172.00000
#> 2        46.82500
#> 3        24.40000
#> 4        20.41667
#> 5         8.10000
#> 6       134.60000
#> 7        10.20000
#> 8       187.90000
#> 9        92.00000
#> 10             NA

# You can see how the data.frame has been returned with a new column (NIAmg_combined)
# and an additional comment in the comments column, detailing the calculation used.

# However, what if you don't have data for all of these nutrients? In that case,
# The nutrient in question should be set to NA. The function assumes you have
# all nutrients it needs available, and assumes the standard INFOODS TAGNAMEunit
# naming system. This is why the above example works without the need to specify
# which column is which. However, when it a column is missing, this means that
# the default input needs to be overridden by an NA value, to tell the function that
# its missing.

breakfast_df$TRPmg <- NULL

breakfast_df
#>    food_code      food_name NIAmg NIAEQmg NIATRPmg
#> 1      F0001          Bacon 172.0    85.3     2.10
#> 2      F0002          Beans    NA    49.2       NA
#> 3      F0003          Toast  24.4    86.4     2.38
#> 4      F0004       Mushroom    NA    23.2     2.80
#> 5      F0005           Eggs   8.1    30.5       NA
#> 6      F0006         Tomato 134.6    83.3     1.27
#> 7      F0007        Sausage  10.2    16.6     1.64
#> 8      F0008         Butter 187.9    84.5     0.69
#> 9      F0009    Brown Sauce  92.0    17.7     2.87
#> 10     F0010 Tomato Ketchup    NA      NA       NA
#>                          comments
#> 1                                
#> 2  These are imaginary food items
#> 3                            <NA>
#> 4  With imaginary nutrient values
#> 5                                
#> 6                      And blanks
#> 7                            <NA>
#> 8       To test different outputs
#> 9                                
#> 10                  And scenarios

# We can see the breakfast_df is now missing an input column; NIATRPmg. To run
# this function without this input, the command would be:

NIA_results_noTRP <- NIAmg_calc_combiner(breakfast_df, TRPmg_column = NA)
#> ---------------------------
#> 
#> Breakdown of values used:
#> 
#> NIAmg_combined equal to NIAEQmg - NIATRPmg 
#>                                          1 
#>              NIAmg_combined equal to NIAmg 
#>                                          7 
#> No suitable value for NIAmg_combined found 
#>                                          2 
#> 
#> ---------------------------
NIA_results_noTRP
#>    food_code      food_name NIAmg NIAEQmg NIATRPmg
#> 1      F0001          Bacon 172.0    85.3     2.10
#> 2      F0002          Beans    NA    49.2       NA
#> 3      F0003          Toast  24.4    86.4     2.38
#> 4      F0004       Mushroom    NA    23.2     2.80
#> 5      F0005           Eggs   8.1    30.5       NA
#> 6      F0006         Tomato 134.6    83.3     1.27
#> 7      F0007        Sausage  10.2    16.6     1.64
#> 8      F0008         Butter 187.9    84.5     0.69
#> 9      F0009    Brown Sauce  92.0    17.7     2.87
#> 10     F0010 Tomato Ketchup    NA      NA       NA
#>                                                                      comments
#> 1                                               NIAmg_combined equal to NIAmg
#> 2  These are imaginary food items; No suitable value for NIAmg_combined found
#> 3                                               NIAmg_combined equal to NIAmg
#> 4  With imaginary nutrient values; NIAmg_combined equal to NIAEQmg - NIATRPmg
#> 5                                               NIAmg_combined equal to NIAmg
#> 6                                   And blanks; NIAmg_combined equal to NIAmg
#> 7                                               NIAmg_combined equal to NIAmg
#> 8                    To test different outputs; NIAmg_combined equal to NIAmg
#> 9                                               NIAmg_combined equal to NIAmg
#> 10                  And scenarios; No suitable value for NIAmg_combined found
#>    NIAmg_combined
#> 1           172.0
#> 2              NA
#> 3            24.4
#> 4            20.4
#> 5             8.1
#> 6           134.6
#> 7            10.2
#> 8           187.9
#> 9            92.0
#> 10             NA

# You can see the results have shifted. The values that had been calculated using
# TRP are now calculated using NIATRP. (Please don't worry about the discrepancy
# between the previous results and these; these nutritional figures are entirely
# fictional, and so the new calculation may shift NIAmg_combined massively. This
# is not an issue.)

# If too few inputs for calculation are inputted, then the function will show a
# warning, and not run.


# The third example uses non-standard names, to show how to set the input parameters
# if the data.frame is not using the suggested TAGNAMEunit naming system.

breakfast_df_nonstandard <- breakfast_df_nonstandard[,c("food_code",
"food_name", "Niacin_milligrams", "Tryptophan_milligrams", "Niacin_eq_milligrams",
"Niacine_from_TRP_mg", "comments_column")]

breakfast_df_nonstandard
#>    food_code      food_name Niacin_milligrams Tryptophan_milligrams
#> 1      F0001          Bacon             172.0                 126.0
#> 2      F0002          Beans                NA                 142.5
#> 3      F0003          Toast              24.4                 142.7
#> 4      F0004       Mushroom                NA                 167.0
#> 5      F0005           Eggs               8.1                    NA
#> 6      F0006         Tomato             134.6                  76.3
#> 7      F0007        Sausage              10.2                  98.6
#> 8      F0008         Butter             187.9                  41.4
#> 9      F0009    Brown Sauce              92.0                 172.0
#> 10     F0010 Tomato Ketchup                NA                    NA
#>    Niacin_eq_milligrams Niacine_from_TRP_mg                comments_column
#> 1                  85.3                2.10                               
#> 2                  49.2                  NA These are imaginary food items
#> 3                  86.4                2.38                           <NA>
#> 4                  23.2                2.80 With imaginary nutrient values
#> 5                  30.5                  NA                               
#> 6                  83.3                1.27                     And blanks
#> 7                  16.6                1.64                           <NA>
#> 8                  84.5                0.69      To test different outputs
#> 9                  17.7                2.87                               
#> 10                   NA                  NA                  And scenarios
# You can see this is the same dataset as used previously, but with differing
# column names. This will mean the function will not know what the required
# column names are, and will need the user to name them.

NIA_results_nonstandard <- NIAmg_calc_combiner(breakfast_df_nonstandard,
NIAmg_column = "Niacin_milligrams",
TRPmg_column = "Tryptophan_milligrams",
NIAEQmg_column = "Niacin_eq_milligrams",
NIATRPmg_column = "Niacine_from_TRP_mg",
comment_col = "comments_column")
#> ---------------------------
#> 
#> Breakdown of values used:
#> 
#> NIAmg_combined equal to Niacin_eq_milligrams - (Tryptophan_milligrams/60) 
#>                                                                         2 
#>                                 NIAmg_combined equal to Niacin_milligrams 
#>                                                                         7 
#>                                No suitable value for NIAmg_combined found 
#>                                                                         1 
#> 
#> ---------------------------

NIA_results_nonstandard
#>    food_code      food_name Niacin_milligrams Tryptophan_milligrams
#> 1      F0001          Bacon             172.0                 126.0
#> 2      F0002          Beans                NA                 142.5
#> 3      F0003          Toast              24.4                 142.7
#> 4      F0004       Mushroom                NA                 167.0
#> 5      F0005           Eggs               8.1                    NA
#> 6      F0006         Tomato             134.6                  76.3
#> 7      F0007        Sausage              10.2                  98.6
#> 8      F0008         Butter             187.9                  41.4
#> 9      F0009    Brown Sauce              92.0                 172.0
#> 10     F0010 Tomato Ketchup                NA                    NA
#>    Niacin_eq_milligrams Niacine_from_TRP_mg
#> 1                  85.3                2.10
#> 2                  49.2                  NA
#> 3                  86.4                2.38
#> 4                  23.2                2.80
#> 5                  30.5                  NA
#> 6                  83.3                1.27
#> 7                  16.6                1.64
#> 8                  84.5                0.69
#> 9                  17.7                2.87
#> 10                   NA                  NA
#>                                                                                              comments_column
#> 1                                                                  NIAmg_combined equal to Niacin_milligrams
#> 2  These are imaginary food items; NIAmg_combined equal to Niacin_eq_milligrams - (Tryptophan_milligrams/60)
#> 3                                                                  NIAmg_combined equal to Niacin_milligrams
#> 4  With imaginary nutrient values; NIAmg_combined equal to Niacin_eq_milligrams - (Tryptophan_milligrams/60)
#> 5                                                                  NIAmg_combined equal to Niacin_milligrams
#> 6                                                      And blanks; NIAmg_combined equal to Niacin_milligrams
#> 7                                                                  NIAmg_combined equal to Niacin_milligrams
#> 8                                       To test different outputs; NIAmg_combined equal to Niacin_milligrams
#> 9                                                                  NIAmg_combined equal to Niacin_milligrams
#> 10                                                 And scenarios; No suitable value for NIAmg_combined found
#>    NIAmg_combined
#> 1       172.00000
#> 2        46.82500
#> 3        24.40000
#> 4        20.41667
#> 5         8.10000
#> 6       134.60000
#> 7        10.20000
#> 8       187.90000
#> 9        92.00000
#> 10             NA

# You can see how the results are the same as calculated above, regardless of
# the changed column names.