Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

problem with using coalesce to merge 2 columns into 1

I would like to merge values from 2 columns into 1. For example, here is sample data:

id   x    y
1   12  
1         14  
2   13      
3   15 
3         18
4         19

I want

id   x    y     z
1   12          12  
1         14    14  
2   13          13 
3   15          15 
3         18    18
4         19    19

I tried using coalesce to create a new variable.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

coalesce <- function(...) {
  apply(cbind(...), 1, function(x) {
    x[which(!is.na(x))[1]]
  })
}

df$z <- coalesce(df$x, df$y)

However, the variable doesn’t reflect the columns joined. Am I using this function incorrectly?

>Solution :

You could use the dplyr::coalesce function:

> df$z <- dplyr::coalesce(ifelse(df$x == "", NA, df$x), df$y)
> df
  id  x  y  z
1  1 12    12
2  1    14 14
3  2 13    13
4  3 15    15
5  3    18 18
6  4    19 19
> 

To implement my own mycoalesce:

mycoalesce <- function(...) {apply(cbind(...), 1, max)}

And:

> df$z <- mycoalesce(df$x, df$y)
> df
  id  x  y  z
1  1 12    12
2  1    14 14
3  2 13    13
4  3 15    15
5  3    18 18
6  4    19 19
> 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading