2

My Data looks like this:

      x  y  z
    1 na na 4
    2 na na 5
    3 na 4  na
    4 na 2  na
    5 7  na na
    6 9  na na

I tried paste and merge, but then the output looks like this:

      x
    1 na na 4
    2 na na 4
    3 na  4 na

I want numbers only and I want them in only one column. This is what I want:

      x 
    1 4
    2 5
    3 4
    4 2
    5 7
    6 9

Do you guys know how to do that? You would be a great help to me! Thank you!

Mala Dhyan
  • 23
  • 3
  • 3
    @akrun [You can instantly reopen any question closed as a duplicate that was originally asked with a tag you have a gold badge for](https://meta.stackexchange.com/a/231212/220122). [if you have a gold tag-badge for a tag associated with a question that's been closed as a duplicate, you can edit the duplicate links to replace, add, remove or re-arrange them](https://meta.stackexchange.com/q/291824/220122). – Henrik May 08 '19 at 19:46
  • 1
    @anjama that post has several answers that use either `dplyr::coalesce` or other implementations of the same type of function. The answers become complicated because part of the question was about measuring efficiency, but the solutions themselves should cover it – camille May 08 '19 at 19:54

1 Answers1

4

One option would be coalesce

library(dplyr)
df1  %>%
   transmute(x = coalesce(x, y, z))

assuming that nas are real NAs (missing values) and not character strings

akrun
  • 874,273
  • 37
  • 540
  • 662
  • It worked, but it's not numeric anymore. And the as.numeric code shows me this error: (list) object cannot be coerced to type 'double'. How do I change that? – Mala Dhyan May 08 '19 at 20:16
  • @MalaDhyan Could be that your columns were not numeric to start with. I would use `df1 %>% mutate_all(as.numeric) %>% transmute(x = coalesce(x, y, z))` – akrun May 08 '19 at 20:18