'Selecting elements from list based on length in R

enter image description hereI have this list of ~270'000 elements with each element having a length of either 165 or 166. What I would like is to isolate into a separate dataset those elements with length of 165.



Solution 1:[1]

Try using lengths :

result <- data2[lengths(data2) == 165]

Few other options include :

result <- Filter(function(x) length(x) == 165, data2)
result <- purrr::keep(data2, ~length(.x) == 165)
result <- purrr::discard(data2, ~length(.x) != 165)

Solution 2:[2]

Does this work:

set.seed(1)
mylist <- list(sample(1:10, 165, T), sample(1:10, 166, T), sample(1:10, 165, T), sample(1:10, 166, T))


l1 <- mylist[sapply(mylist, function(x) length(x) == 165)]
l2 <- mylist[sapply(mylist, function(x) length(x) == 166)]

Solution 3:[3]

We can use:

data2[sapply(mylist, length) == 165]

Solution 4:[4]

How to select 2 items combined list in the apriori function:

rules <- apriori(Groceries,parameter = list(support = 0.01, confidence = 0.01, minlen = 2, maxlen = 2))

summary(rules3)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ronak Shah
Solution 2 Karthik S
Solution 3 Elletlar
Solution 4 Martin Gal