1

I have:

inputDT <- data.table(COL1 = c(1, NA, NA), COL1 = c(NA, 2, NA), COL1 = c(NA, NA, 3))
inputDT
   COL1 COL1 COL1
1:    1   NA   NA
2:   NA    2   NA
3:   NA   NA    3

I want

outputDT <- data.table(COL1 = c(1,2,3))
outputDT
   COL1
1:    1
2:    2
3:    3

Essentially, I have a data.table with multiple columns whose names are the same (values are mutually exclusive), and I need to generate just one column to combine those.

How to achieve it?

1
  • 1
    Does this answer your question? R: coalescing a large data frame ; You should find a solution there, except if you want to avoid calling dplyr; you can otherwise also call it within DT. Commented Mar 8, 2020 at 22:59

2 Answers 2

7

The OP is asking for a data.table solution. As of version v1.12.4 (03 Oct 2019), the fcoalesce() function is available:

library(data.table)
inputDT[, .(COL1 = fcoalesce(.SD))]
   COL1
1:    1
2:    2
3:    3
Sign up to request clarification or add additional context in comments.

1 Comment

Some of my columns have text fields, and this approach works.
1

Alternatively (less elegant than @Uwe's answer), if you have only numbers and NA, you can calculate the max of each row while removing NA:

library(data.table)
inputDT[, .(COL2 = do.call(pmax, c(na.rm=TRUE, .SD)))]

  COL2
1:    1
2:    2
3:    3

5 Comments

Thanks ! I try but when I called back inputDT I get the former data.table.
An alternative to do.call() is reduce(): inputDT[, .(COL2 = purrr::reduce(.SD, pmax, na.rm = TRUE))]. purrr::reduce() is like base R's Reduce() but has the advantage that you can pass additional arguments to the function without having to write an anonymous function.
@Uwe, thanks for the tip. I don't know much about purr package, trying to learn it (slowly).
@Uwe the current solution is passing additional arguments to pmax() without having to write an anonymous function.
@sindri_baldur, Yes, indeed. My comment concerning passing additional arguments does not refer to dc37's answer but refers solely to my suggestion of an alternative approach to use purrr::reduce() instead of do.call(). I was discussing the advantages of purrr::reduce() vs. Reduce().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.