Replace values in a named vector while preserving order and other elements.
replace_values.Rd
This function takes a source named vector (value
) and a named vector of
replacements. It replaces the values in value
where the names
match those in replacement
, while keeping all other values and their
original order intact.
Arguments
- value
A named atomic vector (e.g., numeric, character, logical). This is the vector whose values will be modified. Must have names.
- replacement
A named atomic vector of the same type as
value
(or coercible). The names in this vector specify which elements invalue
to replacement, and its values are the new values to be assigned. Must have names.
Value
A named vector with the updated values. The order and names of
elements not specified in replacement
remain unchanged from value
.
Examples
# Example usage:
value <- c(a=2, b=3, d=5, foo=66)
replace <- c(foo=0, b=77)
new_value <- replace_values(value, replace)
print(new_value)
#> a b d foo
#> 2 77 5 0
# Expected output: a=2, b=77, d=5, foo=0
# Example with some names in 'replacement' not present in 'value'
value2 <- c(x=10, y=20)
replacement2 <- c(y=50, z=100) # 'z' is not in src2
new_value2 <- replace_values(value2, replacement2)
#> Warning: The following names in 'replacement' were not found in 'value' and were ignored: z
print(new_value2)
#> x y z
#> 10 50 100
# Expected output: x=10, y=50, with a warning about 'z'
# Example with missing names in value or replacement (will throw an error)
# tryCatch(replace_values(c(1,2), c(a=3)), error = function(e) message(e$message))
# tryCatch(replace_values(c(a=1, b=2), c(3,4)), error = function(e) message(e$message))