find index in a list of list

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

find index in a list of list

zadig
Hi all,

I have a list of lists like this :

mylist <- list(list(a=10,b="x"),list(a=11,b="y"),list(a=12,b="z"))

I want to find the index of list in mylist where a = 11 and b  = "y"  , so I want to get 2 as a result

Thanks in advance

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: find index in a list of list

Rui Barradas
Hello,

Something like this?

find <- list(a=11,b="y")
which(sapply(mylist, identical, find))

Hope this helps,

Rui Barradas


Em 11-03-2017 14:59, ce escreveu:

> Hi all,
>
> I have a list of lists like this :
>
> mylist <- list(list(a=10,b="x"),list(a=11,b="y"),list(a=12,b="z"))
>
> I want to find the index of list in mylist where a = 11 and b  = "y"  , so I want to get 2 as a result
>
> Thanks in advance
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: find index in a list of list

zadig
In reply to this post by zadig

Exactly. Thanks a lot, I was trying sapply with to result.

-----Original Message-----
From: "Rui Barradas" [[hidden email]]
Date: 03/11/2017 10:06 AM
To: "ce" <[hidden email]>, [hidden email]
Subject: Re: [R] find index in a list of list

Hello,

Something like this?

find <- list(a=11,b="y")
which(sapply(mylist, identical, find))

Hope this helps,

Rui Barradas


Em 11-03-2017 14:59, ce escreveu:

> Hi all,
>
> I have a list of lists like this :
>
> mylist <- list(list(a=10,b="x"),list(a=11,b="y"),list(a=12,b="z"))
>
> I want to find the index of list in mylist where a = 11 and b  = "y"  , so I want to get 2 as a result
>
> Thanks in advance
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: find index in a list of list

zadig
In reply to this post by zadig


Sorry I rejoiced  too soon. In fact original list is more complex like :

mylist <- list(list(a=10,b="x",c=1),list(a=11,b="y",c=2),list(a=12,b="z",c=5))

and I still need to find index of where a = 11 and b = "y"  and I have no c value ,

-----Original Message-----
From: "ce" [[hidden email]]
Date: 03/11/2017 10:13 AM
To: [hidden email], "Rui Barradas" <[hidden email]>
Subject: Re: [R] find index in a list of list


Exactly. Thanks a lot, I was trying sapply with to result.

-----Original Message-----
From: "Rui Barradas" [[hidden email]]
Date: 03/11/2017 10:06 AM
To: "ce" <[hidden email]>, [hidden email]
Subject: Re: [R] find index in a list of list

Hello,

Something like this?

find <- list(a=11,b="y")
which(sapply(mylist, identical, find))

Hope this helps,

Rui Barradas


Em 11-03-2017 14:59, ce escreveu:

> Hi all,
>
> I have a list of lists like this :
>
> mylist <- list(list(a=10,b="x"),list(a=11,b="y"),list(a=12,b="z"))
>
> I want to find the index of list in mylist where a = 11 and b  = "y"  , so I want to get 2 as a result
>
> Thanks in advance
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: find index in a list of list

Jeff Newmiller
Since the offered solution already checks each top level element using the "identical" function, you just need a different comparison function, like perhaps:

mycompare <- function( x, y ) {
   identical( x[[ "a" ]], y[[ "a" ]] ) && identical( x[[ "b" ]], y[[ "b" ]] )
}

Note that your decision to store this data in a list of lists is making this search process much less computationally and syntactically efficient than it would be if you could fit your data into a data frame.
--
Sent from my phone. Please excuse my brevity.

On March 11, 2017 7:17:11 AM PST, ce <[hidden email]> wrote:

>
>
>Sorry I rejoiced  too soon. In fact original list is more complex like
>:
>
>mylist <-
>list(list(a=10,b="x",c=1),list(a=11,b="y",c=2),list(a=12,b="z",c=5))
>
>and I still need to find index of where a = 11 and b = "y"  and I have
>no c value ,
>
>-----Original Message-----
>From: "ce" [[hidden email]]
>Date: 03/11/2017 10:13 AM
>To: [hidden email], "Rui Barradas" <[hidden email]>
>Subject: Re: [R] find index in a list of list
>
>
>Exactly. Thanks a lot, I was trying sapply with to result.
>
>-----Original Message-----
>From: "Rui Barradas" [[hidden email]]
>Date: 03/11/2017 10:06 AM
>To: "ce" <[hidden email]>, [hidden email]
>Subject: Re: [R] find index in a list of list
>
>Hello,
>
>Something like this?
>
>find <- list(a=11,b="y")
>which(sapply(mylist, identical, find))
>
>Hope this helps,
>
>Rui Barradas
>
>
>Em 11-03-2017 14:59, ce escreveu:
>> Hi all,
>>
>> I have a list of lists like this :
>>
>> mylist <- list(list(a=10,b="x"),list(a=11,b="y"),list(a=12,b="z"))
>>
>> I want to find the index of list in mylist where a = 11 and b  = "y"
>, so I want to get 2 as a result
>>
>> Thanks in advance
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: find index in a list of list

zadig
In reply to this post by zadig

Thank you both, indeed combining two solutions fixed my problem :

> which(sapply(mylist, mycompare,find))
[1] 2

I admit list of lists is not the optimal design,  it's out of my control but is there is not much data in it, so performance is not an  issue.
For the sake of R's honour I didn't want to use "for loop".

ce


-----Original Message-----
From: "Jeff Newmiller" [[hidden email]]
Date: 03/11/2017 11:23 AM
To: [hidden email], "ce" <[hidden email]>, "Rui Barradas" <[hidden email]>
Subject: Re: [R] find index in a list of list

Since the offered solution already checks each top level element using the "identical" function, you just need a different comparison function, like perhaps:

mycompare <- function( x, y ) {
   identical( x[[ "a" ]], y[[ "a" ]] ) && identical( x[[ "b" ]], y[[ "b" ]] )
}

Note that your decision to store this data in a list of lists is making this search process much less computationally and syntactically efficient than it would be if you could fit your data into a data frame.
--
Sent from my phone. Please excuse my brevity.

On March 11, 2017 7:17:11 AM PST, ce <[hidden email]> wrote:

>
>
>Sorry I rejoiced  too soon. In fact original list is more complex like
>:
>
>mylist <-
>list(list(a=10,b="x",c=1),list(a=11,b="y",c=2),list(a=12,b="z",c=5))
>
>and I still need to find index of where a = 11 and b = "y"  and I have
>no c value ,
>
>-----Original Message-----
>From: "ce" [[hidden email]]
>Date: 03/11/2017 10:13 AM
>To: [hidden email], "Rui Barradas" <[hidden email]>
>Subject: Re: [R] find index in a list of list
>
>
>Exactly. Thanks a lot, I was trying sapply with to result.
>
>-----Original Message-----
>From: "Rui Barradas" [[hidden email]]
>Date: 03/11/2017 10:06 AM
>To: "ce" <[hidden email]>, [hidden email]
>Subject: Re: [R] find index in a list of list
>
>Hello,
>
>Something like this?
>
>find <- list(a=11,b="y")
>which(sapply(mylist, identical, find))
>
>Hope this helps,
>
>Rui Barradas
>
>
>Em 11-03-2017 14:59, ce escreveu:
>> Hi all,
>>
>> I have a list of lists like this :
>>
>> mylist <- list(list(a=10,b="x"),list(a=11,b="y"),list(a=12,b="z"))
>>
>> I want to find the index of list in mylist where a = 11 and b  = "y"
>, so I want to get 2 as a result
>>
>> Thanks in advance
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.