# A tibble: 5 × 3
var_with_na var_non_na my_fixed_var
<dbl> <dbl> <dbl>
1 NA 1.4 1.4
2 5.4 5 5.4
3 NA 9.4 9.4
4 13.4 13 13.4
5 NA 17.4 17.4
A more efficient way to do this is to use the function coalesce() from the dplyr package. This will allow you to find the first non-missing element in a set of vectors.
# A tibble: 5 × 3
var_with_na var_non_na my_fixed_var
<dbl> <dbl> <dbl>
1 NA 1.4 1.4
2 5.4 5 5.4
3 NA 9.4 9.4
4 13.4 13 13.4
5 NA 17.4 17.4
This function takes all the available values from the vector you set in the first argument and replaces its non-available values with the first non-missing values from the vector in the second argument.
You could also do this with more than two vectors.
# A tibble: 5 × 4
var_with_na_1 var_with_na_2 var_with_na_3 my_fixed_var
<dbl> <dbl> <dbl> <dbl>
1 NA 1.2 1.4 1.2
2 5.4 5.2 NA 5.4
3 NA NA NA NA
4 13.4 NA 13 13.4
5 NA NA 17.4 17.4
The sequence of the vectors specified as arguments in the coalesce function determines the order in which the NA values of the initial vectors will be replaced with the values of the remaining ones. Remember that the first non-missing element in a set of vectors will be taken as a replacement value for the NA values in the first vectors. If all values are not available, then the result will also be.
Citation
BibTeX citation:
@online{lettry2024,
author = {Lettry, Layal Christine},
title = {How to Replace Some Non-Available Values in a Vector with
Values Coming from Another Vector?},
date = {2024-05-10},
url = {https://rdiscovery.netlify.app/posts/2024-05-10_coalesce/},
langid = {en}
}