How to combine two vectors while replacing the NA values with the values in the other vector in R?

R ProgrammingServer Side ProgrammingProgramming

Sometimes we have vectors with NA values, also there might be a situation that one of vector having an NA at a position and the other vector has the numerical values at the same position. For example, 1, 2, NA and 1, 2, 3. In this case, we might want to combine these two vectors to make a single vector. This can be done by using coalesce function of dplyr package.

Example

> library(dplyr)
> x1<-sample(c(1:4,NA),20,replace=TRUE)
> x1

Output

[1] NA 4 NA 1 2 NA 4 1 4 1 2 3 1 4 2 2 NA 2 2 1

Example

 Live Demo

> y1<-1:20
> y1

Output

[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> coalesce(x1,y1)

Output

[1] 1 4 3 1 2 6 4 1 4 1 2 3 1 4 2 2 17 2 2 1

Example

 Live Demo

> x2<-sample(c(5,NA,10),50,replace=TRUE)
> x2

Output

[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

Example

 Live Demo

> y2<-sample(c(5,8,10),50,replace=TRUE)
> y2

Output

[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)

Output

[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

Example

 Live Demo

> x3<-sample(c(2,4,16,NA,10),100,replace=TRUE)
> x3

Output

[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

Example

 Live Demo

> y3<-sample(c(2,4,16,8,10),100,replace=TRUE)
> y3

Output

[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)

Output

[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

Example

 Live Demo

> x4<-sample(rnorm(10),50,replace=TRUE)
> x4

Output

[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

Example

 Live Demo

> y4<-sample(c(rnorm(10),NA),50,replace=TRUE)
> y4

Output

[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)

Output

[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

Example

 Live Demo

> x5<-sample(c(runif(10,2,5)),50,replace=TRUE)
> x5

Output

[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

Example

 Live Demo

> y5<-sample(c(runif(10,3,5)),50,replace=TRUE)
> y5

Output

[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)

Output

[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
raja
Updated on 07-Sep-2020 06:17:22

Advertisements