[R] counting row repetitions without loop

James Foadi james.foadi at diamond.ac.uk
Wed Feb 6 15:53:12 CET 2008


On Wednesday 06 February 2008 14:08, Waterman, DG (David) wrote:
> Hi,
>
> I have a data frame consisting of coordinates on a 10*10 grid, i.e.
>
> > example
>
>     x  y
> 1   4  5
> 2   6  7
> 3   6  6
> 4   7  5
> 5   5  7
> 6   6  7
> 7   4  5
> 8   6  7
> 9   7  6
> 10  5  6
>
> What I would like to do is return an 10*10 matrix consisting of counts
> at each position, so in the above example I would have a matrix where,
> for example, cell [4,5] contains 2 and [6,7] contains 3. At the moment I
> have implemented this using a for loop over the rows of the data frame,
> however the data frames I want to process are very long so the loop
> takes many minutes to complete. Can I do this in a more efficient way?
>
> Cheers,


David,
have a look at "mapply" (?mapply). This does what you need very quickly.

J



More information about the R-help mailing list