I have a list of lists in R:
a <- list(x=0, y=c(1,2,3), z=4)
b <- list(x=1, y=c(1,2,3), z=44)
c <- list(x=2, y=c(1,2,3), z=444)
L <- list(a,b,c)
For a given list, say
l <- list(x=0, y=c(1,2,3), z=4)
I know want to find the correct index of L
where we find the corresponding list that equals l
.
Of course, I can use a loop, but since L
is very large, I need a faster solution.
(And is a list even the right choice here?)
The list data structure in R allows the user to store homogeneous (of the same type) or heterogeneous (of different types) R objects. Therefore, a list can contain objects of any type including lists themselves.
How to Create Lists in R? We can use the list() function to create a list. Another way to create a list is to use the c() function. The c() function coerces elements into the same type, so, if there is a list amongst the elements, then all elements are turned into components of a list.
A vector is a one dimensional array of elements. Vectors are the basic building blocks of R. Almost all data in R is stored in a vector, or even a vector of vectors. A list is a recursive vector: a vector that can contain another vector or list in each of its elements.
The list() function in R is used to create a list of elements of different types. A list can contain numeric, string, or vector elements.
R list can also contain a matrix or a function as its elements. The list is created using the list() function in R. In other words, a list is a generic vector containing other objects. For example: The variable x is containing copies of three vectors n, s, b and a numeric value 3.
Compare two lists in R You can compare two lists in different ways. This include obtaining the common elements, the different elements or comparing the equal ones. Before each explanation we are going to copy our first list and change its first element.
Two or more R lists can be joined together. For that purpose, you can use the append, the c or the do.call functions. When combining the lists this way, the second list elements will be appended at the end of the first list.
Then we can use the match R function as follows: The match function returns the value 2; The value 5 was found at the second position of our example vector. Note: The match command returned only the first match, even though the value 5 matches also the fourth element of our example vector.
We can use stack
with identical
from base R
which(sapply(L, function(x) identical(stack(l), stack(x))))
#[1] 1
Or more compactly
which(sapply(L, identical, l))
#[1] 1
Using mapply
to compare each element one by one with l
. If you unlist
it, all
should be TRUE
. Using which
around an sapply
finally gives the number of the matching element.
f <- function(x) all(unlist(mapply(`==`, x, l)))
which(sapply(L, f))
# [1] 1
Data:
L <- list(list(x = 0, y = c(1, 2, 3), z = 4), list(x = 1, y = c(1,
2, 3), z = 44), list(x = 2, y = c(1, 2, 3), z = 444))
l <- list(x = 0, y = c(1, 2, 3), z = 4)
Perhaps you can try mapply
like below
> which(mapply(identical, L, list(l)))
[1] 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With