Hello, I have been struggling for quite some time to find a solution for the following problem. I have a data frame which is organized by block and trial. Each trial is represented across several rows in this data frame. I'd like to extract the first x rows per trial and block. For example block trial x y 1 1 1 605 150 2 1 1 603 148 3 1 1 604 140 4 1 1 600 140 5 1 1 590 135 6 1 1 580 135 7 1 2 607 148 8 1 2 605 152 10 1 2 600 158 ..... Selecting only the the first two rows per trial should result in block trial x y 1 1 605 150 1 1 603 148 1 2 607 148 1 2 605 152 The data I am dealing with a x-y coordinates (samples) from an eye-tracking experiment. I receive the data in this format and need to eliminate unwanted samples. Thanks Jens B?lte
try this, library(plyr) ddply(d, .(block, trial), function(.d) .d[1:2, ])> block trial x y > 1 1 1 605 150 > 2 1 1 603 148 > 3 1 2 607 148 > 4 1 2 605 152HTH, baptiste On 11 May 2009, at 13:49, Jens B?lte wrote:> Hello, > > I have been struggling for quite some time to find a solution for > the following problem. I have a data frame which is organized by > block and trial. Each trial is represented across several rows in > this data frame. I'd like to extract the first x rows per trial and > block. > > For example > block trial x y > 1 1 1 605 150 > 2 1 1 603 148 > 3 1 1 604 140 > 4 1 1 600 140 > 5 1 1 590 135 > 6 1 1 580 135 > 7 1 2 607 148 > 8 1 2 605 152 > 10 1 2 600 158 > ..... > > Selecting only the the first two rows per trial should result in > > block trial x y > 1 1 605 150 > 1 1 603 148 > 1 2 607 148 > 1 2 605 152 > > The data I am dealing with a x-y coordinates (samples) from an eye- > tracking experiment. I receive the data in this format and need to > eliminate unwanted samples. > > Thanks Jens B?lte > <ATT00001.txt>_____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag
Try this: do.call(rbind, by(DF, DF[1:2], head, 2)) On Mon, May 11, 2009 at 7:49 AM, Jens B?lte <boelte at psy.uni-muenster.de> wrote:> Hello, > > I have been struggling for quite some time to find a solution for the > following problem. I have a data frame which is organized by block and > trial. Each trial is represented across several rows in this data frame. I'd > like to extract the first x rows per trial and block. > > For example > ? ? ? ?block ? trial ? x ? ? ? y > 1 ? ? ? 1 ? ? ? 1 ? ? ? 605 ? ? 150 > 2 ? ? ? 1 ? ? ? 1 ? ? ? 603 ? ? 148 > 3 ? ? ? 1 ? ? ? 1 ? ? ? 604 ? ? 140 > 4 ? ? ? 1 ? ? ? 1 ? ? ? 600 ? ? 140 > 5 ? ? ? 1 ? ? ? 1 ? ? ? 590 ? ? 135 > 6 ? ? ? 1 ? ? ? 1 ? ? ? 580 ? ? 135 > 7 ? ? ? 1 ? ? ? 2 ? ? ? 607 ? ? 148 > 8 ? ? ? 1 ? ? ? 2 ? ? ? 605 ? ? 152 > 10 ? ? ?1 ? ? ? 2 ? ? ? 600 ? ? 158 > ..... > > Selecting only the the first two rows per trial should result in > block trial x y 1 ? ? ? 1 ? ? ? 605 ? ? 150 > 1 ? ? ? 1 ? ? ? 603 ? ? 148 > 1 ? ? ? 2 ? ? ? 607 ? ? 148 > 1 ? ? ? 2 ? ? ? 605 ? ? 152 > > The data I am dealing with a x-y coordinates (samples) from an eye-tracking > experiment. I receive the data in this format and need to eliminate unwanted > samples. > > Thanks Jens B?lte > > ______________________________________________ > 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. > >
Here is a way of doing it:> xblock trial x y 1 1 1 605 150 2 1 1 603 148 3 1 1 604 140 4 1 1 600 140 5 1 1 590 135 6 1 1 580 135 7 1 2 607 148 8 1 2 605 152 10 1 2 600 158> do.call(rbind, lapply(split(x, list(x$block, x$trial), drop=TRUE), head,2)) block trial x y 1.1.1 1 1 605 150 1.1.2 1 1 603 148 1.2.7 1 2 607 148 1.2.8 1 2 605 152 On Mon, May 11, 2009 at 7:49 AM, Jens Bölte <boelte@psy.uni-muenster.de>wrote:> Hello, > > I have been struggling for quite some time to find a solution for the > following problem. I have a data frame which is organized by block and > trial. Each trial is represented across several rows in this data frame. I'd > like to extract the first x rows per trial and block. > > For example > block trial x y > 1 1 1 605 150 > 2 1 1 603 148 > 3 1 1 604 140 > 4 1 1 600 140 > 5 1 1 590 135 > 6 1 1 580 135 > 7 1 2 607 148 > 8 1 2 605 152 > 10 1 2 600 158 > ..... > > Selecting only the the first two rows per trial should result in > block trial x y 1 1 605 150 > 1 1 603 148 > 1 2 607 148 > 1 2 605 152 > > The data I am dealing with a x-y coordinates (samples) from an eye-tracking > experiment. I receive the data in this format and need to eliminate unwanted > samples. > > Thanks Jens Bölte > > ______________________________________________ > 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. > >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
Seemingly Similar Threads
- How to number a subset consecutively from 1 to n?
- selectively altering variable value
- re: Btrfs: fix num_workers_starting bug and other bugs in async thread
- Re: *** buffer overflow detected *** accessing invalid FD in libguestfs
- *** buffer overflow detected *** accessing invalid FD in libguestfs