Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Providing the correct match for a list in a list of lists in R

Tags:

r

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 Lis very large, I need a faster solution. (And is a list even the right choice here?)

like image 995
Claudio Moneo Avatar asked Jan 17 '21 17:01

Claudio Moneo


People also ask

Can you store a list within a list in R?

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 do you make a list of lists in R?

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.

Can you have a vector of lists in R?

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.

Which function for lists in R?

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.

What is a list in R with example?

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.

How do you compare two lists in R?

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.

How do you join two lists together in R?

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.

How to use the match R function?

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.


3 Answers

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
like image 132
akrun Avatar answered Nov 15 '22 06:11

akrun


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)
like image 27
jay.sf Avatar answered Nov 15 '22 06:11

jay.sf


Perhaps you can try mapply like below

> which(mapply(identical, L, list(l)))
[1] 1
like image 28
ThomasIsCoding Avatar answered Nov 15 '22 05:11

ThomasIsCoding