Mohammad Tanvir Ahamed
2016-Jun-28 07:03 UTC
[R] Get the location of a numeric element in a list
Can any one please help me. I will apply this for a very large list, about 400k vector in a list and vector size is unequal and large Example : Input: a <- c(1,3,6,9,25,100) b<-c(10,7,20,2,25) c<-c(1,7,5,15,25,300,1000) d<-list(a,b,c) Expected outcome : # When looking for 1 in d c(1,3) # When looking for 7 in d c(2,3) # when looking for 25 in d c(1,2,3) # When looking for 50 in d NULL or 0 Thanks in advance !! Tanvir Ahamed G?teborg, Sweden | mashranga at yahoo.com
Hi Tanvir, How about this: value<-1 (1:length(d))[unlist(lapply(lapply(d,"==",value),any))] Jim On Tue, Jun 28, 2016 at 5:03 PM, Mohammad Tanvir Ahamed via R-help <r-help at r-project.org> wrote:> Can any one please help me. I will apply this for a very large list, about 400k vector in a list and vector size is unequal and large > > Example : > Input: > a <- c(1,3,6,9,25,100) > b<-c(10,7,20,2,25) > c<-c(1,7,5,15,25,300,1000) > d<-list(a,b,c) > > Expected outcome : > # When looking for 1 in d > c(1,3) > > # When looking for 7 in d > > c(2,3) > > # when looking for 25 in d > c(1,2,3) > # When looking for 50 in d > NULL or 0 > > > Thanks in advance !! > > > > > > Tanvir Ahamed > G?teborg, Sweden | mashranga at yahoo.com > > ______________________________________________ > R-help at r-project.org 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.
Hi This could give you desired result, however I am not sure about performance or elegancy. which(sapply((lapply(d, "==", 1)), any)) Or you can transform it to a function fff <- function(x,a) which(sapply((lapply(x, "==", a)), any))> fff(d, 25)[1] 1 2 3> fff(d, 5)[1] 3> fff(d, 1)[1] 1 3> fff(d, 50)integer(0)>Beware also the trap of floating point representation if your numbers are not integer (FAQ 7.31) Cheers Petr> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of > Mohammad Tanvir Ahamed via R-help > Sent: Tuesday, June 28, 2016 9:03 AM > To: R-help Mailing List <r-help at r-project.org> > Subject: [R] Get the location of a numeric element in a list > > Can any one please help me. I will apply this for a very large list, about 400k > vector in a list and vector size is unequal and large > > Example : > Input: > a <- c(1,3,6,9,25,100) > b<-c(10,7,20,2,25) > c<-c(1,7,5,15,25,300,1000) > d<-list(a,b,c) > > Expected outcome : > # When looking for 1 in d > c(1,3) > > # When looking for 7 in d > > c(2,3) > > # when looking for 25 in d > c(1,2,3) > # When looking for 50 in d > NULL or 0 > > > Thanks in advance !! > > > > > > Tanvir Ahamed > G?teborg, Sweden | mashranga at yahoo.com > > ______________________________________________ > R-help at r-project.org 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.________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
On 06/28/2016 03:03 AM, Mohammad Tanvir Ahamed via R-help wrote:> Can any one please help me. I will apply this for a very large list, about 400k vector in a list and vector size is unequal and large > > Example : > Input: > a <- c(1,3,6,9,25,100) > b<-c(10,7,20,2,25) > c<-c(1,7,5,15,25,300,1000) > d<-list(a,b,c) > > Expected outcome : > # When looking for 1 in d > c(1,3) > > # When looking for 7 in d > > c(2,3) > > # when looking for 25 in d > c(1,2,3) > # When looking for 50 in d > NULL or 0Make a vector of queries queries = c(1, 7, 25, 50) Create a factor of unlist(d), using queries as levels. Create a vector rep(seq_along(d), lengths(d)), and split it into groups defined by f f = factor(unlist(d, use.names=FALSE), levels=queries) split(rep(seq_along(d), lengths(d)), f) Martin Morgan> > > Thanks in advance !! > > > > > > Tanvir Ahamed > G?teborg, Sweden | mashranga at yahoo.com > > ______________________________________________ > R-help at r-project.org 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. >This email message may contain legally privileged and/or...{{dropped:2}}
Mohammad Tanvir Ahamed
2016-Jun-28 12:26 UTC
[R] Get the location of a numeric element in a list
Thanks Martin Morgan. Thats works excellent on a large data . Tanvir Ahamed G?teborg, Sweden | mashranga at yahoo.com ----- Original Message ----- From: Martin Morgan <martin.morgan at roswellpark.org> To: Mohammad Tanvir Ahamed <mashranga at yahoo.com>; R-help Mailing List <r-help at r-project.org> Sent: Tuesday, 28 June 2016, 11:07 Subject: Re: [R] Get the location of a numeric element in a list On 06/28/2016 03:03 AM, Mohammad Tanvir Ahamed via R-help wrote:> Can any one please help me. I will apply this for a very large list, about 400k vector in a list and vector size is unequal and large > > Example : > Input: > a <- c(1,3,6,9,25,100) > b<-c(10,7,20,2,25) > c<-c(1,7,5,15,25,300,1000) > d<-list(a,b,c) > > Expected outcome : > # When looking for 1 in d > c(1,3) > > # When looking for 7 in d > > c(2,3) > > # when looking for 25 in d > c(1,2,3) > # When looking for 50 in d > NULL or 0Make a vector of queries queries = c(1, 7, 25, 50) Create a factor of unlist(d), using queries as levels. Create a vector rep(seq_along(d), lengths(d)), and split it into groups defined by f f = factor(unlist(d, use.names=FALSE), levels=queries) split(rep(seq_along(d), lengths(d)), f) Martin Morgan> > > Thanks in advance !! > > > > > > Tanvir Ahamed > G?teborg, Sweden | mashranga at yahoo.com > > ______________________________________________ > R-help at r-project.org 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. >This email message may contain legally privileged and/or confidential information. If you are not the intended recipient(s), or the employee or agent responsible for the delivery of this message to the intended recipient(s), you are hereby notified that any disclosure, copying, distribution, or use of this email message is prohibited. If you have received this message in error, please notify the sender immediately by e-mail and delete this email message from your computer. Thank you.