You can use the is.na()
function to identify the missing values in vector A and then use the logical index to replace the missing values with the corresponding values from vector B. At the same time, you can set those values in vector B to NA using the same logical index. Here is an example:
# Example vectors
A <- c(1, 2, NA, 4, NA, 6)
B <- c(10, 20, 30, 40, 50, 60)
# Identify missing values in A
missing_A <- is.na(A)
# Replace missing values in A with values from B and set corresponding values in B to NA
B[missing_A] <- NA
A[missing_A] <- B[missing_A]
# Print result
A
# [1] 1 2 30 4 50 6
B
# [1] 10 20 NA 40 NA 60
In this example, the missing values in vector A are replaced with the corresponding values from vector B (30
and 50
), while those values in vector B are set to NA
.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-02-16 11:00:00 +0000
Seen: 10 times
Last updated: Nov 09 '21
How to perform batch geocoding when longitude and latitude values are missing?
How can a new object with a specific type be created in Angular 2 using the domain model?
What is the method to eliminate NA from facet_wrap in ggplot2?
How can a text/varchar column be shortened when duplicate values are not permitted?
How are `all: unset` and `all: revert` dissimilar from each other?
How can I set values on a Map using more than one parameter from a nativeQuery?
What is the problem encountered when attempting to filter the column values of a data.frame?
How can an array be filtered using the values in another array?