Hello Community,
I have a large dataset to which I need to apply a list of hundreds of pre-defined edits. The edit conditions are stored in a table and need to be run sequentially.
I have tried unsuccessfully to paste the conditions as strings into nested IFELSE functions. It works if I hardcode each one manually, but I need it to run from the central list of edit conditions.
What is a good method of importing the list of edits automatically and then applying them to the data iteratively?
Thanks for any help.
##########################################################################
# Here are some examples of edit conditions.
# 1. IF var1 == 0 THEN var1 = var2 ELSE do nothing
# 2. IF var2 > 0 & var3 > 0 THEN var1 = var2/var3 ELSE var2 = var3
# 3. IF abs(var2-var3)/var4 > 0.2 THEN var1 = 0 & var2 = var3 ELSE var2 = 0 & var3 = var1
# The format of the table can be changed if need be.
edit_conditions <-
tibble::tribble(
~EditID, ~IF_Condition, ~THEN_Function, ~ELSE_Function,
"E1", "var1 == 0", "var1 = sum(var2, var3)", "var1 = var1",
"E2", "var2 > 0 & var3 > 0", "var1 = var2/var3", "var2 = var3",
"E3", "abs(var2-var3)/var4 > 0.2", "var1 = 0 & var2 = var3", "var2 = 0 & var3 = var1"
)
##########################################################################
# Here is some test data.
data <-
tibble::tribble(
~UnitID, ~var1, ~var2, ~var3, ~var4,
1L, 0L, 0L, 20L, 9L,
2L, 1L, 1L, 21L, 8L,
3L, 0L, 2L, 22L, 7L,
4L, 1L, 3L, 23L, 6L,
5L, 0L, 4L, 24L, 5L,
6L, 1L, 5L, 25L, 4L,
7L, 0L, 6L, 26L, 6L,
8L, 1L, 7L, 27L, 7L,
9L, 0L, 8L, 28L, 8L,
10L, 1L, 9L, 29L, 9L
)