Replace Values in Column in Rows Matching Condition
Arguments
- df
data frame in which to do substitutions
- file
path to CSV file with columns
group
,target_column
,condition
,replacement
- group
group name. If given, only rows in
file
that have this group name in columngroup
are considered.- config
optional. Data frame containing the configuration as being read from
file
.- dbg
if
TRUE
debug messages are shown
Examples
# Create a very simple data frame
df <- data.frame(a = 1:3)
# Create a very simple configuration
config <- read.table(sep = ",", header = TRUE, text = c(
"group,target,condition,replacement",
"g1,a,a==2,22",
"g2,a,a==3,33"
))
# Write the configuration to a temporary file
file <- tempfile()
write.csv(config, file)
# Apply all replacements configured in the file ...
replaceByCondition(df, file)
#> Data correction in 'a': 1 values with "a==2" set to '22'
#> Data correction in 'a': 1 values with "a==3" set to '33'
#> a
#> 1 1
#> 2 22
#> 3 33
# ... or in the configuration
replaceByCondition(df, config = config)
#> Data correction in 'a': 1 values with "a==2" set to '22'
#> Data correction in 'a': 1 values with "a==3" set to '33'
#> a
#> 1 1
#> 2 22
#> 3 33
# Apply selected replacements
replaceByCondition(df, file, group = "g1")
#> Data correction in 'a': 1 values with "a==2" set to '22'
#> a
#> 1 1
#> 2 22
#> 3 3
replaceByCondition(df, file, group = "g2")
#> Data correction in 'a': 1 values with "a==3" set to '33'
#> a
#> 1 1
#> 2 2
#> 3 33