Warm tip: This article is reproduced from stackoverflow.com, please click
data.table list r unique

Identify same lists in list (as variable) in a data.table

发布于 2020-03-28 23:13:11

I arranged my data in a data.table and created the column livar that contains a list or sometimes a list with lists. Each row represents multiple records for a unique record. The column livar aggregates all observations of a variable (not in the data.table anymore) of a unique record in a list or when there are multiple entries for a record then in a list of lists.

I want to check now if all the lists of one row in the column livar are the same. In the code provided I expected the first row in the same column to be FALSE and the second and third to be TRUE. As the following lines show the code works as I expected but not in the data.table context. Can anybody help here?

a <- c(123456, NA, 456789, NA, NA)
b <- c(NA, NA)
c <- c(123456, NA, 987654, NA, NA)
d <- c(123456, NA, 987654, NA, NA)
e <- c(NA, NA)

li1 <- list(a, b)
li2 <- list(c, d)

DT <- data.table(c(1,2,3))
DT[, livar := list(li1, li2, e)]
DT[, same := length(unique(livar))==1] # FALSE

length(unique(li1))==1 # FALSE
length(unique(li2))==1 # TRUE
length(unique(e))==1 # TRUE
length(unique(c(NA, NA)))==1 # TRUE
Questioner
yaennu.s
Viewed
78
yaennu.s 2020-01-31 17:27

As chinsoon12 pointed out a *apply was missing in the following line in order to get into each list of the list.

DT[, same := sapply(livar, function(x) length(unique(x))==1L)]