如何在将NA值替换为R中另一个向量中的值的同时合并两个向量?
有时我们有带有NA值的向量,也可能存在这样的情况:一个向量在某个位置具有NA,而另一个向量在同一位置具有数值。例如1、2,NA和1、2、3。在这种情况下,我们可能希望将这两个向量组合在一起以形成一个向量。这可以通过使用dplyr软件包的合并功能来完成。
示例
> library(dplyr) > x1<-sample(c(1:4,NA),20,replace=TRUE) > x1
输出结果
[1] NA 4 NA 1 2 NA 4 1 4 1 2 3 1 4 2 2 NA 2 2 1
示例
> y1<-1:20 > y1
输出结果
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> coalesce(x1,y1)
输出结果
[1] 1 4 3 1 2 6 4 1 4 1 2 3 1 4 2 2 17 2 2 1
示例
> x2<-sample(c(5,NA,10),50,replace=TRUE) > x2
输出结果
[1] 10 5 NA 5 5 NA 5 NA NA 5 NA 10 NA 10 5 NA NA NA 10 5 5 NA NA 5 NA [26] 5 10 5 10 10 10 10 NA NA 5 NA 5 NA 5 NA 10 10 NA 10 NA 5 NA 10 10 5
示例
> y2<-sample(c(5,8,10),50,replace=TRUE) > y2
输出结果
[1] 5 5 10 5 10 8 8 8 5 10 8 5 10 8 10 5 5 8 5 5 10 8 8 8 10 [26] 5 10 5 5 10 10 5 5 10 10 8 10 10 8 8 10 5 5 5 8 5 10 5 8 8
> coalesce(x2,y2)
输出结果
[1] 10 5 10 5 5 8 5 8 5 5 8 10 10 10 5 5 5 8 10 5 5 8 8 5 10 [26] 5 10 5 10 10 10 10 5 10 5 8 5 10 5 8 10 10 5 10 8 5 10 10 10 5
示例
> x3<-sample(c(2,4,16,NA,10),100,replace=TRUE) > x3
输出结果
[1] 2 2 16 16 NA NA 2 16 16 4 4 4 NA 10 2 4 NA 2 4 NA 16 2 10 10 16 [26] NA 2 16 2 10 NA 4 2 10 10 16 10 16 10 NA 4 4 4 4 10 NA NA 10 16 10 [51] 4 4 NA 16 16 2 NA 10 16 16 16 16 2 4 16 2 2 16 NA 2 NA 4 16 4 16 [76] 2 4 NA NA 2 2 10 16 10 4 10 4 10 NA 10 NA 4 10 4 16 2 4 2 16 2
示例
> y3<-sample(c(2,4,16,8,10),100,replace=TRUE) > y3
输出结果
[1] 10 2 8 8 10 8 10 8 4 2 10 4 16 10 8 2 8 8 2 2 4 4 2 10 10 [26] 8 16 8 16 4 4 2 16 2 16 8 4 16 4 8 8 10 10 4 4 4 4 2 4 10 [51] 2 8 2 4 2 10 8 8 4 4 2 10 2 16 8 8 10 8 2 2 10 16 10 10 2 [76] 4 10 2 4 10 4 8 16 4 2 4 2 16 4 8 10 4 8 10 16 4 4 4 10 2
> coalesce(x3,y3)
输出结果
[1] 2 2 16 16 10 8 2 16 16 4 4 4 16 10 2 4 8 2 4 2 16 2 10 10 16 [26] 8 2 16 2 10 4 4 2 10 10 16 10 16 10 8 4 4 4 4 10 4 4 10 16 10 [51] 4 4 2 16 16 2 8 10 16 16 16 16 2 4 16 2 2 16 2 2 10 4 16 4 16 [76] 2 4 2 4 2 2 10 16 10 4 10 4 10 4 10 10 4 10 4 16 2 4 2 16 2
示例
> x4<-sample(rnorm(10),50,replace=TRUE) > x4
输出结果
[1] -0.02879712 1.31733536 -0.02879712 -0.77268752 0.47775338 1.23117578 [7] 1.23117578 -0.77268752 1.31733536 0.30903710 0.47775338 1.31733536 [13] -0.77268752 0.30903710 1.31733536 -1.65681391 -1.65681391 1.31733536 [19] -0.02879712 0.47775338 0.30903710 -0.77268752 0.08699075 -0.02879712 [25] -1.65681391 -0.77268752 0.45328379 1.23117578 0.08699075 -1.65681391 [31] 1.23117578 0.47775338 1.25889501 0.45328379 1.31733536 -1.65681391 [37] 0.45328379 0.30903710 0.45328379 1.31733536 0.30903710 0.45328379 [43] 0.08699075 1.31733536 -1.65681391 1.31733536 -0.77268752 -1.65681391 [49] -0.77268752 1.25889501
示例
> y4<-sample(c(rnorm(10),NA),50,replace=TRUE) > y4
输出结果
[1] -0.6383008 -0.6383008 1.0152569 1.0152569 0.5242708 -0.6383008 [7] -0.3965653 NA 0.5242708 -0.4664776 NA NA [13] 1.1300389 -0.3965653 -0.3965653 1.0152569 -0.9956721 -0.4664776 [19] -0.4664776 -0.3610621 NA 0.4749585 -0.4664776 -0.3965653 [25] -0.3610621 -0.9956721 -0.6383008 NA 1.0152569 -0.4664776 [31] 0.4749585 NA -0.4664776 -0.9956721 NA -0.3610621 [37] -0.3965653 1.0152569 0.4749585 0.5242708 0.5242708 -0.3965653 [43] -0.4664776 -0.8300846 -0.9956721 NA 1.0152569 -0.9956721 [49] -0.4664776 -0.6383008
> coalesce(x4,y4)
输出结果
[1] -0.02879712 1.31733536 -0.02879712 -0.77268752 0.47775338 1.23117578 [7] 1.23117578 -0.77268752 1.31733536 0.30903710 0.47775338 1.31733536 [13] -0.77268752 0.30903710 1.31733536 -1.65681391 -1.65681391 1.31733536 [19] -0.02879712 0.47775338 0.30903710 -0.77268752 0.08699075 -0.02879712 [25] -1.65681391 -0.77268752 0.45328379 1.23117578 0.08699075 -1.65681391 [31] 1.23117578 0.47775338 1.25889501 0.45328379 1.31733536 -1.65681391 [37] 0.45328379 0.30903710 0.45328379 1.31733536 0.30903710 0.45328379 [43] 0.08699075 1.31733536 -1.65681391 1.31733536 -0.77268752 -1.65681391 [49] -0.77268752 1.25889501
示例
> x5<-sample(c(runif(10,2,5)),50,replace=TRUE) > x5
输出结果
[1] 4.791954 4.447842 4.479391 4.627612 4.447842 2.460460 2.460460 4.627612 [9] 4.130456 2.904874 2.904874 2.460460 4.804377 2.517587 4.447842 4.627612 [17] 4.804377 4.791954 4.479391 2.460460 4.479391 4.479391 4.130456 3.218738 [25] 4.447842 4.130456 2.460460 4.791954 4.479391 3.218738 4.479391 4.479391 [33] 4.791954 2.517587 4.447842 4.479391 3.218738 4.130456 3.218738 4.791954 [41] 2.904874 4.479391 4.791954 2.904874 4.479391 4.791954 4.479391 4.130456 [49] 4.804377 4.791954
示例
> y5<-sample(c(runif(10,3,5)),50,replace=TRUE) > y5
输出结果
[1] 4.805609 4.832933 3.553856 4.832933 4.942905 4.805609 4.942905 4.608437 [9] 4.832933 4.163457 4.832933 4.608437 3.013883 4.608437 4.942905 4.163457 [17] 4.743048 3.013883 3.013883 4.743048 3.553856 4.743048 3.933597 3.933597 [25] 4.743048 4.508212 4.805609 4.743048 3.013883 4.163457 4.942905 4.743048 [33] 4.942905 4.508212 3.553856 4.608437 3.553856 4.743048 3.933597 4.163457 [41] 3.933597 4.508212 4.608437 4.508212 3.933597 4.942905 4.508212 4.832933 [49] 4.832933 4.805609
> coalesce(x5,y5)
输出结果
[1] 4.791954 4.447842 4.479391 4.627612 4.447842 2.460460 2.460460 4.627612 [9] 4.130456 2.904874 2.904874 2.460460 4.804377 2.517587 4.447842 4.627612 [17] 4.804377 4.791954 4.479391 2.460460 4.479391 4.479391 4.130456 3.218738 [25] 4.447842 4.130456 2.460460 4.791954 4.479391 3.218738 4.479391 4.479391 [33] 4.791954 2.517587 4.447842 4.479391 3.218738 4.130456 3.218738 4.791954 [41] 2.904874 4.479391 4.791954 2.904874 4.479391 4.791954 4.479391 4.130456 [49] 4.804377 4.791954