简体   繁体   中英

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.

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 ] ]

Here is another data.table option using var

dt[, d := ifelse(var(unlist(.SD)) == 0, "identical", "non_identical"), seq(nrow(dt))]

which gives

> dt
   a b c             d
1: 1 1 1     identical
2: 2 2 1 non_identical

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM