-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.R
36 lines (31 loc) · 899 Bytes
/
functions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#Script that loads all functions
#Fuzzy matcher
source(here::here("functions", "Fuzzy_Matcher.R"))
#Food component calculators
source(here::here("functions", "summary_table_functions.R"))
#Energy calculators
source(here::here("functions", "Energy_Standardisation.R"))
#No brackets
#The following f(x) removes []
no_brackets <- function(i){
case_when(
str_detect(i, '\\[.*?\\]') == TRUE ~ str_extract(i, '(?<=\\[).*?(?=\\])'),
TRUE ~ i)
}
#Replacing "Tr" to zero
TraceToZero <- function(x){
x <- gsub("Trace|trace|Tr|tr|N", "0", x)
return(x)
}
#Remove "*" to avoid conversion to NA
RemoveStar <- function(x){
x <- gsub("\\*", "", x)
return(x)
}
no_brackets_tr_ast <- function(i){
case_when(
str_detect(i, 'tr|[tr]') ~ "0",
str_detect(i, '\\[.*?\\]') ~ str_extract(i, '(?<=\\[).*?(?=\\])'),
str_detect(i, '[*]') ~ str_extract(i, "[:digit:]+"),
TRUE ~ i)
}