oliver1902
oliver1902

Reputation: 33

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.

Upvotes: 0

Views: 1361

Answers (4)

Sai Ma
Sai Ma

Reputation: 1

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)

Upvotes: 0

akrun
akrun

Reputation: 886998

We can use:

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

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388862

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)

Upvotes: 2

Karthik S
Karthik S

Reputation: 11584

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

Upvotes: 0

Related Questions