Search code examples
rdata.tablerowwise

How to check if values in individiual rows of a data.table are identical


Suppose I have the following data.table:

dt <- data.table(a = 1:2, b = 1:2, c = c(1, 1))

# dt
#    a b c
# 1: 1 1 1
# 2: 2 2 1

What would be the fastest way to create a fourth column d indicating that the preexisting values in each row are all identical, so that the resulting data.table will look like the following?

# dt
#    a b c              d
# 1: 1 1 1      identical
# 2: 2 2 1  not_identical

I want to avoid using duplicated function and want to stick to using identical or a similar function even if it means iterating through items within each row.


Solution

  • uniqueN can be applied grouped by row and create a logical expression (== 1)

    library(data.table)
    dt[, d := c("not_identical", "identical")[(uniqueN(unlist(.SD)) == 1) +
           1], 1:nrow(dt)]
    

    -output

    dt
    #   a b c             d
    #1: 1 1 1     identical
    #2: 2 2 1 not_identical
    

    Or another efficient approach might be to do comparison with the first column, and create an expression with rowSums

    dt[, d := c("identical", "not_identical")[1 + rowSums(.SD[[1]] != .SD) > 0 ] ]