... Perhaps also worth mentioning -- David's solution works even if there are less than 3 rows per group, whereas mine will fail.
Cheers, Bert Bert Gunter "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." -- Clifford Stoll On Wed, Dec 2, 2015 at 2:10 PM, Bert Gunter <bgunter.4...@gmail.com> wrote: > ... or perhaps using rep() to do the indexing directly instead of matching: > > dfrm[ ave(dfrm$v1, dfrm$v1, FUN = > function(x)rep(c(TRUE,FALSE),c(3,length(x)-3))), ] > > There's probably another 6 dozen ways to do it, especially if you > access packages like data.table, plyr, etc. > > Cheers, > Bert > > > Bert Gunter > > "Data is not information. Information is not knowledge. And knowledge > is certainly not wisdom." > -- Clifford Stoll > > > On Wed, Dec 2, 2015 at 12:26 PM, David Winsemius <dwinsem...@comcast.net> > wrote: >> >>> On Dec 2, 2015, at 10:09 AM, Ragia Ibrahim <ragi...@hotmail.com> wrote: >>> >>> Dear Group, >>> I have a data frame that such as >>> >>> v1 v2 v3 v4 >>> 1 1 3 6 >>> 1 1 5 6 >>> 1 1 8 0 >>> 1 2 6 1 >>> 1 2 4 0 >>> 1 3 4 4 >>> 1 3 5 4 >>> 1 3 6 3 >>> 1 3 7 1 >>> >>> 2 4 3 7 >>> 2 5 5 4 >>> 2 5 8 2 >>> 2 1 6 1 >>> 2 1 4 0 >>> 2 1 4 3 >>> 2 1 5 2 >>> 3 1 6 1 >>> 3 6 7 0 >>> >>> 3 6 3 6 >>> 3 6 5 6 >>> 3 6 8 0 >>> 3 6 6 1 >>> 3 2 4 0 >>> 3 2 4 4 >>> 3 2 5 4 >>> 3 2 6 3 >>> 3 2 7 1 >>> 3 5 9 5 >>> 3 6 9 5 >>> >>> >>> the result required is each first 3 rows, from distinct v2 column for each >>> v1 column >>> >>> >>> v1 v2 v3 v4 >>> 1 1 3 6 >>> 1 2 6 1 >>> 1 3 4 4 >>> 2 4 3 7 >>> 2 5 5 4 >>> 2 1 6 1 >>> 3 6 3 6 >>> 3 2 4 0 >>> 3 5 9 5 >> >> >> Probably something along the lines of >> >> dfrm[ ave(dfrm$v1, dfrm$v1, FUN=seq_along) %in% 1:3 , ] >> >> >>> >>> >>> thanks in advance >>> [[alternative HTML version deleted]] >> >> Future postings should be in plain text. >> >> — >> >> David Winsemius >> Alameda, CA, USA >> >> ______________________________________________ >> R-help@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. ______________________________________________ R-help@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.