且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 R 中将两列合并为一列

更新时间:2022-01-17 22:38:35

这在写答案时可能不存在,但由于我带着同样的问题来到这里并找到了更好的解决方案,这里是为未来的 googlers 准备的:

This probably didn't exist when the answers were written, but since I came here with the same question and found a better solution, here it is for future googlers:

你想要的是 dplyr 中的 coalesce() 函数:

What you want is the coalesce() function from dplyr:

y <- c(1, 2, NA, NA, 5)
z <- c(NA, NA, 3, 4, 5)
coalesce(y, z)

[1] 1 2 3 4 5