Ragia Ibrahim
2015-Nov-09 10:55 UTC
[R] get maximum 3 rows by column elements in data frame
Dear group, I have the following data freame dput(df_all_nodes) structure(list(Measure_id = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1,? 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2,? 3, 4, 5, 1, 2, 3, 4, 5), i = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2,? 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7,? 7, 7, 7, 7, 7, 7, 7, 7, 7), j = c(1, 1, 1, 1, 1, 3, 3, 3, 3,? 3, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,? 3, 3, 3, 3, 3, 5, 5, 5, 5, 5), value = c(1.5, 2, 1, 0, 2, 2,? 1.5, 0, 0, 1, 1, 2, 0, 1, 2, 2, 0.5, 1, 0, 2, 2, 1.5, 0, 0, 1,? 1.5, 2.5, 0, 1, 2, 1.5, 1, 0, 0, 1, 1, 2, 0, 0, 1), rank = c(0.75,? 1, 1, 0, 1, 1, 0.75, 0, 0, 0.5, 0.5, 1, 0, 1, 1, 1, 1, 1, NaN,? 1, 1, 0.6, NaN, 0, 0.5, 0.75, 1, NaN, 1, 1, 1, 0.5, NaN, NaN,? 1, 0.666666666666667, 1, NaN, NaN, 1)), .Names = c("Measure_id",? "i", "j", "value", "rank"), row.names = c(NA, 40L), class = "data.frame")>?I want to get maximum 3 rows in each group of Measure_id. e.g. for measure_id 1 get the max ranks ?(select the max for each measure depending on the rank column). how to do that Best regards, Ragia
It is not entirely clear what you are asking for. Can you provide a sample of the output that you want from the data. Here is the data split by Measure_id, but not sure what to do with it:> split(x, x$Measure_id)$`1` Measure_id i j value rank 1 1 2 1 1.5 0.7500000 6 1 2 3 2.0 1.0000000 11 1 2 4 1.0 0.5000000 16 1 4 3 2.0 1.0000000 21 1 5 1 2.0 1.0000000 26 1 5 2 1.5 0.7500000 31 1 7 3 1.5 1.0000000 36 1 7 5 1.0 0.6666667 $`2` Measure_id i j value rank 2 2 2 1 2.0 1.00 7 2 2 3 1.5 0.75 12 2 2 4 2.0 1.00 17 2 4 3 0.5 1.00 22 2 5 1 1.5 0.60 27 2 5 2 2.5 1.00 32 2 7 3 1.0 0.50 37 2 7 5 2.0 1.00 $`3` Measure_id i j value rank 3 3 2 1 1 1 8 3 2 3 0 0 13 3 2 4 0 0 18 3 4 3 1 1 23 3 5 1 0 NaN 28 3 5 2 0 NaN 33 3 7 3 0 NaN 38 3 7 5 0 NaN $`4` Measure_id i j value rank 4 4 2 1 0 0 9 4 2 3 0 0 14 4 2 4 1 1 19 4 4 3 0 NaN 24 4 5 1 0 0 29 4 5 2 1 1 34 4 7 3 0 NaN 39 4 7 5 0 NaN $`5` Measure_id i j value rank 5 5 2 1 2 1.0 10 5 2 3 1 0.5 15 5 2 4 2 1.0 20 5 4 3 2 1.0 25 5 5 1 1 0.5 30 5 5 2 2 1.0 35 5 7 3 1 1.0 40 5 7 5 1 1.0>Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Mon, Nov 9, 2015 at 5:55 AM, Ragia Ibrahim <ragia11 at hotmail.com> wrote:> Dear group, > > I have the following data freame > > dput(df_all_nodes) > > structure(list(Measure_id = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, > 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, > 3, 4, 5, 1, 2, 3, 4, 5), i = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, > 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7, > 7, 7, 7, 7, 7, 7, 7, 7, 7), j = c(1, 1, 1, 1, 1, 3, 3, 3, 3, > 3, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, > 3, 3, 3, 3, 3, 5, 5, 5, 5, 5), value = c(1.5, 2, 1, 0, 2, 2, > 1.5, 0, 0, 1, 1, 2, 0, 1, 2, 2, 0.5, 1, 0, 2, 2, 1.5, 0, 0, 1, > 1.5, 2.5, 0, 1, 2, 1.5, 1, 0, 0, 1, 1, 2, 0, 0, 1), rank = c(0.75, > 1, 1, 0, 1, 1, 0.75, 0, 0, 0.5, 0.5, 1, 0, 1, 1, 1, 1, 1, NaN, > 1, 1, 0.6, NaN, 0, 0.5, 0.75, 1, NaN, 1, 1, 1, 0.5, NaN, NaN, > 1, 0.666666666666667, 1, NaN, NaN, 1)), .Names = c("Measure_id", > "i", "j", "value", "rank"), row.names = c(NA, 40L), class = "data.frame") > > > > I want to get maximum 3 rows in each group of Measure_id. e.g. for > measure_id 1 get the max ranks (select the max for each measure depending > on the rank column). > > how to do that > Best regards, > Ragia > > > > ______________________________________________ > 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.[[alternative HTML version deleted]]
Hi I am not completely sure what do you want, so here is my guess.> dat<-structure(list(Measure_id = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5), i = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7), j = c(1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5), value = c(1.5, 2, 1, 0, 2, 2, 1.5, 0, 0, 1, 1, 2, 0, 1, 2, 2, 0.5, 1, 0, 2, 2, 1.5, 0, 0, 1, 1.5, 2.5, 0, 1, 2, 1.5, 1, 0, 0, 1, 1, 2, 0, 0, 1), rank = c(0.75, 1, 1, 0, 1, 1, 0.75, 0, 0, 0.5, 0.5, 1, 0, 1, 1, 1, 1, 1, NaN, 1, 1, 0.6, NaN, 0, 0.5, 0.75, 1, NaN, 1, 1, 1, 0.5, NaN, NaN, 1, 0.666666666666667, 1, NaN, NaN, 1)), .Names = c("Measure_id", "i", "j", "value", "rank"), row.names = c(NA, 40L), class = "data.frame")I named your data dat. First I set decreasing order for Measure Id and rank.> ooo<-order(dat[,1], dat[,5], decreasing=T)I than changed order of rows.> dat<-dat[ooo,]And finally with this oneliner selected 3 rows in each measure_id by rank> do.call(rbind, lapply(split(dat, dat[,1]), head, 3))Measure_id i j value rank 1.6 1 2 3 2.0 1 1.16 1 4 3 2.0 1 1.21 1 5 1 2.0 1 2.2 2 2 1 2.0 1 2.12 2 2 4 2.0 1 2.17 2 4 3 0.5 1 3.3 3 2 1 1.0 1 3.18 3 4 3 1.0 1 3.8 3 2 3 0.0 0 4.14 4 2 4 1.0 1 4.29 4 5 2 1.0 1 4.4 4 2 1 0.0 0 5.5 5 2 1 2.0 1 5.15 5 2 4 2.0 1 5.20 5 4 3 2.0 1 Is this what you wanted? Cheers Petr> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ragia > Ibrahim > Sent: Monday, November 09, 2015 11:55 AM > To: r-help at r-project.org > Subject: [R] get maximum 3 rows by column elements in data frame > > Dear group, > > I have the following data freame > > dput(df_all_nodes) > > structure(list(Measure_id = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, > 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, > 4, 5), i = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, > 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7), j > c(1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 1, 1, 1, > 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5), value = c(1.5, 2, > 1, 0, 2, 2, 1.5, 0, 0, 1, 1, 2, 0, 1, 2, 2, 0.5, 1, 0, 2, 2, 1.5, 0, 0, > 1, 1.5, 2.5, 0, 1, 2, 1.5, 1, 0, 0, 1, 1, 2, 0, 0, 1), rank = c(0.75, > 1, 1, 0, 1, 1, 0.75, 0, 0, 0.5, 0.5, 1, 0, 1, 1, 1, 1, 1, NaN, 1, 1, > 0.6, NaN, 0, 0.5, 0.75, 1, NaN, 1, 1, 1, 0.5, NaN, NaN, 1, > 0.666666666666667, 1, NaN, NaN, 1)), .Names = c("Measure_id", "i", "j", > "value", "rank"), row.names = c(NA, 40L), class = "data.frame") > > > > I want to get maximum 3 rows in each group of Measure_id. e.g. for > measure_id 1 get the max ranks (select the max for each measure > depending on the rank column). > > how to do that > Best regards, > Ragia > > > > ______________________________________________ > 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.