replaceByCondition: Replace Values in Column in Rows Matching Condition

View source: R/utils_berlin.R

replaceByConditionR Documentation

Replace Values in Column in Rows Matching Condition

Description

Replace Values in Column in Rows Matching Condition

Usage

replaceByCondition(df, file = NULL, group = NULL, config = NULL, dbg = TRUE)

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 column group 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)

# ... or in the configuration
replaceByCondition(df, config = config)

# Apply selected replacements
replaceByCondition(df, file, group = "g1")
replaceByCondition(df, file, group = "g2")


KWB-R/kwb.prep documentation built on June 11, 2022, 1:29 p.m.