Dear R users, I have a problem. I don't know how to select the top 10 (n) values for each classes. Thank you! My data is like this: row.names proc cls 7271 568,03338 0,5 7270 554,68458 0,5 7269 510,20638 0,5 7268 485,59969 0,5 7267 421,92852 0,5 7272 410,12101 0,5 3414 409,71429 0,5 3452 402,78699 0,5 3451 401,28114 0,5 3450 361,80607 0,5 3413 360,29883 0,5 3449 352,90043 0,5 3453 339,36457 0,5 7266 331,38278 0,5 3415 327,89374 0,5 3454 324,4335 0,5 3412 291,95631 0,5 4770 274,12649 0,5 3455 274,09326 0,5 6030 264,72652 0,5 4772 264,17632 0,5 6031 261,19403 0,5 3411 255,50239 0,5 4773 241,50886 0,5 3447 237,78135 0,5 3448 232,9554 0,5 4774 232,92985 0,5 7273 229,18868 0,5 6028 225,24249 0,5 11839 220,61201 0,5 4771 218,94856 0,5 6026 218,7 0,5 11838 213,44438 0,5 3410 209,35622 0,5 6027 206,68399 0,5 6029 206,10424 0,5 11837 205,28621 0,5 6025 201,21227 0,5 1042 199,78883 0,5 4775 197,29348 1 7265 195,07017 1 4776 175,90948 1 10725 175,60525 1 10724 173,2342 1 3456 172,44641 1 11589 170,2784 1 7190 169,53567 1 11799 169,19097 1 6024 169,09314 1 3416 168,39827 1 10726 165,25617 1 11840 164,43032 1 11800 162,7957 1 11590 155,31572 1 1043 154,6438 1 11841 153,80017 1 7189 152,26293 1 11588 151,07692 1 11798 151,0658 1 3446 147,0405 1 11836 147,02381 1 7188 142,01954 1 11835 140,95302 1 5994 138,06452 1 3523 137,98974 1 11797 137,57633 1 11728 137,49488 1 7192 136,87805 1 11731 135,10162 1 5995 132,41401 1 3096 131,11658 1 7191 131 1 3525 128,74521 1 11801 128,57672 1 7264 128,24427 1 3099 128,20339 1,5 11377 127,53474 1,5 10948 126,81159 1,5 11834 126,67371 1,5 11378 126,42474 1,5 11842 126,27561 1,5 4777 126,26175 1,5 10949 124,39545 1,5 3524 123,64685 1,5 5993 123,39782 1,5 10943 123,27824 1,5 3409 122,84964 1,5 10942 121,28378 1,5 11730 118,13305 1,5 10702 117,91925 1,5 11796 117,5239 1,5 4769 117,38123 1,5 11587 116,49326 1,5 7223 114,19009 1,5 3396 114,16026 1,5 3097 113,99856 1,5 3526 113,97446 1,5 11376 113,58453 1,5 11591 113,46819 1,5 7163 110,99291 1,5 1044 110,76636 1,5 3457 110,35221 1,5 10944 109,87002 1,5 11802 109,64554 1,5 3100 109,2207 1,5 6023 109,01308 1,5 10947 108,45018 1,5 3098 108,20547 1,5 11593 108,0705 1,5 3458 108,03632 1,5 11592 107,53228 1,5 11729 107,35615 1,5 3095 107,24396 1,5 7222 106,02489 1,5 10945 105,24515 1,5 3101 104,75914 1,5 10946 103,92964 1,5 5199 103,81953 1,5 11832 103,6405 1,5 11333 103,53049 1,5 7220 102,87019 1,5 11379 102,66542 1,5 3104 102,5464 1,5 10941 101,4321 1,5 5198 100,62386 1,5 11375 99,731904 1,5 11803 99,08347 1,5 7187 98,076923 1,5 7221 96,853367 1,5 11356 96,797409 1,5 7167 96,326062 1,5 11715 95,902613 1,5 11727 95,79491 1,5 7166 95,747126 1,5 7253 95,56314 2 10703 95,379713 2 3102 94,146825 2 11732 93,961076 2 5992 93,93088 2 11833 93,902859 2 7224 93,114144 2 3103 93,022202 2 7164 92,4972 2 12180 91,891892 2 11355 90,585242 2 10679 90,491487 2 7252 89,664613 2 3105 89,112093 2 5200 88,798701 2 5203 86,954782 2 11354 86,686174 2 11357 86,655518 2 10940 86,422977 2 11594 86,189863 2 12179 86,122178 2 7263 84,991274 2 10520 84,532374 2 11733 83,963012 2 7225 83,766234 2 11586 83,194864 2 Catalin-Constantin ROIBU Forestry engineer, PhD Forestry Faculty of Suceava Str. Universitatii no. 13, Suceava, 720229, Romania office phone +4 0230 52 29 78, ext. 531 mobile phone +4 0745 53 18 01 +4 0766 71 76 58 FAX: +4 0230 52 16 64 silvic.usv.ro [[alternative HTML version deleted]]
Hi, see function ?head or "[". If DF is you data frame, top n values of DF you can select with head(DF, n) or DF[1:n, ]... Andrija On Sun, Feb 10, 2013 at 11:31 AM, catalin roibu <catalinroibu@gmail.com>wrote:> Dear R users, > I have a problem. I don't know how to select the top 10 (n) values for each > classes. > Thank you! > My data is like this: > row.names proc cls 7271 568,03338 0,5 7270 554,68458 0,5 7269 510,20638 > 0,5 7268 485,59969 0,5 7267 421,92852 0,5 7272 410,12101 0,5 3414 > 409,71429 0,5 3452 402,78699 0,5 3451 401,28114 0,5 3450 361,80607 0,5 > 3413 360,29883 0,5 3449 352,90043 0,5 3453 339,36457 0,5 7266 331,38278 > 0,5 3415 327,89374 0,5 3454 324,4335 0,5 3412 291,95631 0,5 4770 > 274,12649 0,5 3455 274,09326 0,5 6030 264,72652 0,5 4772 264,17632 0,5 > 6031 261,19403 0,5 3411 255,50239 0,5 4773 241,50886 0,5 3447 237,78135 > 0,5 3448 232,9554 0,5 4774 232,92985 0,5 7273 229,18868 0,5 6028 > 225,24249 0,5 11839 220,61201 0,5 4771 218,94856 0,5 6026 218,7 0,5 > 11838 213,44438 0,5 3410 209,35622 0,5 6027 206,68399 0,5 6029 206,10424 > 0,5 11837 205,28621 0,5 6025 201,21227 0,5 1042 199,78883 0,5 4775 > 197,29348 1 7265 195,07017 1 4776 175,90948 1 10725 175,60525 1 10724 > 173,2342 1 3456 172,44641 1 11589 170,2784 1 7190 169,53567 1 11799 > 169,19097 1 6024 169,09314 1 3416 168,39827 1 10726 165,25617 1 11840 > 164,43032 1 11800 162,7957 1 11590 155,31572 1 1043 154,6438 1 11841 > 153,80017 1 7189 152,26293 1 11588 151,07692 1 11798 151,0658 1 3446 > 147,0405 1 11836 147,02381 1 7188 142,01954 1 11835 140,95302 1 5994 > 138,06452 1 3523 137,98974 1 11797 137,57633 1 11728 137,49488 1 7192 > 136,87805 1 11731 135,10162 1 5995 132,41401 1 3096 131,11658 1 7191 > 131 > 1 3525 128,74521 1 11801 128,57672 1 7264 128,24427 1 3099 128,20339 > 1,5 > 11377 127,53474 1,5 10948 126,81159 1,5 11834 126,67371 1,5 11378 > 126,42474 1,5 11842 126,27561 1,5 4777 126,26175 1,5 10949 124,39545 1,5 > 3524 123,64685 1,5 5993 123,39782 1,5 10943 123,27824 1,5 3409 122,84964 > 1,5 10942 121,28378 1,5 11730 118,13305 1,5 10702 117,91925 1,5 11796 > 117,5239 1,5 4769 117,38123 1,5 11587 116,49326 1,5 7223 114,19009 1,5 > 3396 114,16026 1,5 3097 113,99856 1,5 3526 113,97446 1,5 11376 113,58453 > 1,5 11591 113,46819 1,5 7163 110,99291 1,5 1044 110,76636 1,5 3457 > 110,35221 1,5 10944 109,87002 1,5 11802 109,64554 1,5 3100 109,2207 1,5 > 6023 109,01308 1,5 10947 108,45018 1,5 3098 108,20547 1,5 11593 108,0705 > 1,5 3458 108,03632 1,5 11592 107,53228 1,5 11729 107,35615 1,5 3095 > 107,24396 1,5 7222 106,02489 1,5 10945 105,24515 1,5 3101 104,75914 1,5 > 10946 103,92964 1,5 5199 103,81953 1,5 11832 103,6405 1,5 11333 > 103,53049 > 1,5 7220 102,87019 1,5 11379 102,66542 1,5 3104 102,5464 1,5 10941 > 101,4321 1,5 5198 100,62386 1,5 11375 99,731904 1,5 11803 99,08347 1,5 > 7187 98,076923 1,5 7221 96,853367 1,5 11356 96,797409 1,5 7167 96,326062 > 1,5 11715 95,902613 1,5 11727 95,79491 1,5 7166 95,747126 1,5 7253 > 95,56314 2 10703 95,379713 2 3102 94,146825 2 11732 93,961076 2 5992 > 93,93088 2 11833 93,902859 2 7224 93,114144 2 3103 93,022202 2 7164 > 92,4972 2 12180 91,891892 2 11355 90,585242 2 10679 90,491487 2 7252 > 89,664613 2 3105 89,112093 2 5200 88,798701 2 5203 86,954782 2 11354 > 86,686174 2 11357 86,655518 2 10940 86,422977 2 11594 86,189863 2 12179 > 86,122178 2 7263 84,991274 2 10520 84,532374 2 11733 83,963012 2 7225 > 83,766234 2 11586 83,194864 2 > > > Catalin-Constantin ROIBU > Forestry engineer, PhD > Forestry Faculty of Suceava > Str. Universitatii no. 13, Suceava, 720229, Romania > office phone +4 0230 52 29 78, ext. 531 > mobile phone +4 0745 53 18 01 > +4 0766 71 76 58 > FAX: +4 0230 52 16 64 > silvic.usv.ro > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
You should have used dput(). Assuming the "," are "." #Using a subset of your example dat1<-read.table(text=" proc cls 7271 568,03338 0,5 ? 7270 554,68458 0,5 ? 7269 510,20638 0,5 ? 7268 485,59969 0,5 ? 7267 421,92852 0,5 ? 7272 410,12101 0,5 ? 3414 409,71429 0,5 ? 3452 402,78699 0,5 ? 3451 401,28114 0,5 ? 3450 361,80607 0,5 ? 3413 360,29883 0,5 ? 3449 352,90043 0,5 ? 3453 339,36457 0,5 ? 7266 331,38278 0,5 ? 3415 327,89374 0,5 ? 3454 324,4335 0,5 ? 3412 291,95631 0,5 ? 4770 274,12649 0,5 ? 3455 274,09326 0,5 ? 6030 264,72652 0,5 ? 4772 264,17632 0,5 ?6031 261,19403 0,5 ? 3411 255,50239 0,5 ?4775 197,29348 1 ? 7265 195,07017 1 ? 4776 175,90948 1 ? 10725 175,60525 1 ? 10724 173,2342 1 ? 3456 172,44641 1 ? 11589 170,2784 1 ? 7190 169,53567 1 ? 11799 169,19097 1 ? 6024 169,09314 1 ? 3416 168,39827 1 ? 10726 165,25617 1 ? 11840 164,43032 1 ? 11800 162,7957 1 ? 11590 155,31572 1 ? 1043 154,6438 1 ? 11841 153,80017 1 ?3099 128,20339 1,5 11377 127,53474 1,5 ? 10948 126,81159 1,5 ? 11834 126,67371 1,5 ? 11378 126,42474 1,5 ? 11842 126,27561 1,5 ? 4777 126,26175 1,5 ? 10949 124,39545 1,5 3524 123,64685 1,5 ? 5993 123,39782 1,5 ? 10943 123,27824 1,5 ? 3409 122,84964 1,5 ? 10942 121,28378 1,5 ? 11730 118,13305 1,5 ?10702 117,91925 1,5 7253 95,56314 2 ? 10703 95,379713 2 ? 3102 94,146825 2 ? 11732 93,961076 2 ? 5992 93,93088 2 ? 11833 93,902859 2 ? 7224 93,114144 2 ? 3103 93,022202 2 ? 7164 92,4972 2 ? 12180 91,891892 2 ? 11355 90,585242 2 ? 10679 90,491487 2 ",header=TRUE,sep="",dec=",")? library(plyr) ddply(dat1,.(cls),head,5) #??????? proc cls #1? 568.03338 0.5 #2? 554.68458 0.5 #3? 510.20638 0.5 #4? 485.59969 0.5 #5? 421.92852 0.5 #6? 197.29348 1.0 #7? 195.07017 1.0 #8? 175.90948 1.0 #9? 175.60525 1.0 #10 173.23420 1.0 #11 128.20339 1.5 #12 127.53474 1.5 #13 126.81159 1.5 #14 126.67371 1.5 #15 126.42474 1.5 #16? 95.56314 2.0 #17? 95.37971 2.0 #18? 94.14683 2.0 #19? 93.96108 2.0 #20? 93.93088 2.0 #or ?do.call(rbind,lapply(split(dat1,dat1$cls),head,5)) A.K. ----- Original Message ----- From: catalin roibu <catalinroibu at gmail.com> To: r-help at r-project.org Cc: Sent: Sunday, February 10, 2013 5:31 AM Subject: [R] top 10 (n values) for each classes Dear R users, I have a problem. I don't know how to select the top 10 (n) values for each classes. Thank you! My data is like this: row.names proc cls? 7271 568,03338 0,5? 7270 554,68458 0,5? 7269 510,20638 0,5? 7268 485,59969 0,5? 7267 421,92852 0,5? 7272 410,12101 0,5? 3414 409,71429 0,5? 3452 402,78699 0,5? 3451 401,28114 0,5? 3450 361,80607 0,5 3413 360,29883 0,5? 3449 352,90043 0,5? 3453 339,36457 0,5? 7266 331,38278 0,5? 3415 327,89374 0,5? 3454 324,4335 0,5? 3412 291,95631 0,5? 4770 274,12649 0,5? 3455 274,09326 0,5? 6030 264,72652 0,5? 4772 264,17632 0,5 6031 261,19403 0,5? 3411 255,50239 0,5? 4773 241,50886 0,5? 3447 237,78135 0,5? 3448 232,9554 0,5? 4774 232,92985 0,5? 7273 229,18868 0,5? 6028 225,24249 0,5? 11839 220,61201 0,5? 4771 218,94856 0,5? 6026 218,7 0,5 11838 213,44438 0,5? 3410 209,35622 0,5? 6027 206,68399 0,5? 6029 206,10424 0,5? 11837 205,28621 0,5? 6025 201,21227 0,5? 1042 199,78883 0,5? 4775 197,29348 1? 7265 195,07017 1? 4776 175,90948 1? 10725 175,60525 1? 10724 173,2342 1? 3456 172,44641 1? 11589 170,2784 1? 7190 169,53567 1? 11799 169,19097 1? 6024 169,09314 1? 3416 168,39827 1? 10726 165,25617 1? 11840 164,43032 1? 11800 162,7957 1? 11590 155,31572 1? 1043 154,6438 1? 11841 153,80017 1? 7189 152,26293 1? 11588 151,07692 1? 11798 151,0658 1? 3446 147,0405 1? 11836 147,02381 1? 7188 142,01954 1? 11835 140,95302 1? 5994 138,06452 1? 3523 137,98974 1? 11797 137,57633 1? 11728 137,49488 1? 7192 136,87805 1? 11731 135,10162 1? 5995 132,41401 1? 3096 131,11658 1? 7191 131 1? 3525 128,74521 1? 11801 128,57672 1? 7264 128,24427 1? 3099 128,20339 1,5 11377 127,53474 1,5? 10948 126,81159 1,5? 11834 126,67371 1,5? 11378 126,42474 1,5? 11842 126,27561 1,5? 4777 126,26175 1,5? 10949 124,39545 1,5 3524 123,64685 1,5? 5993 123,39782 1,5? 10943 123,27824 1,5? 3409 122,84964 1,5? 10942 121,28378 1,5? 11730 118,13305 1,5? 10702 117,91925 1,5? 11796 117,5239 1,5? 4769 117,38123 1,5? 11587 116,49326 1,5? 7223 114,19009 1,5 3396 114,16026 1,5? 3097 113,99856 1,5? 3526 113,97446 1,5? 11376 113,58453 1,5? 11591 113,46819 1,5? 7163 110,99291 1,5? 1044 110,76636 1,5? 3457 110,35221 1,5? 10944 109,87002 1,5? 11802 109,64554 1,5? 3100 109,2207 1,5 6023 109,01308 1,5? 10947 108,45018 1,5? 3098 108,20547 1,5? 11593 108,0705 1,5? 3458 108,03632 1,5? 11592 107,53228 1,5? 11729 107,35615 1,5? 3095 107,24396 1,5? 7222 106,02489 1,5? 10945 105,24515 1,5? 3101 104,75914 1,5 10946 103,92964 1,5? 5199 103,81953 1,5? 11832 103,6405 1,5? 11333 103,53049 1,5? 7220 102,87019 1,5? 11379 102,66542 1,5? 3104 102,5464 1,5? 10941 101,4321 1,5? 5198 100,62386 1,5? 11375 99,731904 1,5? 11803 99,08347 1,5 7187 98,076923 1,5? 7221 96,853367 1,5? 11356 96,797409 1,5? 7167 96,326062 1,5? 11715 95,902613 1,5? 11727 95,79491 1,5? 7166 95,747126 1,5? 7253 95,56314 2? 10703 95,379713 2? 3102 94,146825 2? 11732 93,961076 2? 5992 93,93088 2? 11833 93,902859 2? 7224 93,114144 2? 3103 93,022202 2? 7164 92,4972 2? 12180 91,891892 2? 11355 90,585242 2? 10679 90,491487 2? 7252 89,664613 2? 3105 89,112093 2? 5200 88,798701 2? 5203 86,954782 2? 11354 86,686174 2? 11357 86,655518 2? 10940 86,422977 2? 11594 86,189863 2? 12179 86,122178 2? 7263 84,991274 2? 10520 84,532374 2? 11733 83,963012 2? 7225 83,766234 2? 11586 83,194864 2 Catalin-Constantin ROIBU Forestry engineer, PhD Forestry Faculty of Suceava Str. Universitatii no. 13, Suceava, 720229, Romania office phone? ? +4 0230 52 29 78, ext. 531 mobile phone? +4 0745 53 18 01 ? ? ? ? ? ? ? ? ? ? ? +4 0766 71 76 58 FAX:? ? ? ? ? ? ? ? +4 0230 52 16 64 silvic.usv.ro ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list 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.
On Sun, Feb 10, 2013 at 5:31 AM, catalin roibu <catalinroibu at gmail.com> wrote:> Dear R users, > I have a problem. I don't know how to select the top 10 (n) values for each > classes.Assuming that by top n values in each group you mean the first n rows in each group then: n <- 5 dat1[ave(dat1$cls, dat1$cls, FUN = seq_along) <= n, ] -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com