Kppatel Patel Kppatel Patel - 2 months ago 5
R Question

Check List to find out all values are same in R

I have list as follows

l = list(c("a", "b", "c"), c("a", "b", "c"), c("a", "b", "c"))


I want to check that each of them contain same values using apply family functions.

I want following answer

TRUE, TRUE, TRUE

Answer

We can use duplicated

duplicated(l)|duplicated(l, fromLast=TRUE)
#[1] TRUE TRUE TRUE

If we need to compare all the combinations of list elements, combn is another way

combn(seq_along(l), 2, FUN= function(x) all(l[[x[1]]] == l[[x[2]]]))
#[1] TRUE TRUE TRUE