This function adds or updates a key-value pair in a data frame. If the key already exists in the data frame, its value is updated. If the key does not exist, a new row is added with the specified key and value.

set_key_value(x, key_name, value)

Arguments

x

A data frame containing at least two columns: `key` and `value`. If `x` is `NULL`, a new data frame will be created.

key_name

A character string specifying the key to set or update.

value

A character string specifying the value to associate with the key.

Value

A data frame with the updated or newly added key-value pair. If `x` was `NULL`, a new data frame is returned.

Examples

# Create an empty data frame and add a key-value pair
df <- NULL
df <- set_key_value(df, "name", "Alice")

# Update the value for an existing key
df <- set_key_value(df, "name", "Bob")

# Add another key-value pair
df <- set_key_value(df, "age", "30")