R语言 coalesce 函数

两个主要功能,

替换NA

x <- c(2, 1, NA, 5, 3, NA)       # Create example vector
coalesce(x, 999)                 # Apply coalesce function
# 2   1 999   5   3 999

x中的NA被替换成了999。

对比并替换

这个功能更有用。

y <- c(1, NA, 7, NA, 7, NA)      # Create second example vector
coalesce(x, y)                   # Match two vectors
# 2  1  7  5  3 NA

这里把xy进行对比,并把x中的NA用对应位置的y元素替换

原文地址:https://www.cnblogs.com/yaos/p/14014128.html