[R] placing labels in polygon center ?

Richard A. O'Keefe ok at cs.otago.ac.nz
Fri Aug 15 02:06:52 CEST 2003

"Petr Pikal" <petr.pikal at precheza.cz> wrote (embedded in much XML):
	Not sure about efficency and it is not very general solution but
	you maybe can use embed() function

It's an interesting suggestion, but I don't see *how*.

Here's the function I want, only I'd like it to be something built in.
I've limited it to the where the rotation count is non-negative, just
to keep it simple.  For the same reason, I've limited it to vectors,
although the operation can be generalised in useful ways to matrices
and arrays with any number of subscripts.

    rotate <- function (x, count=1) {
	x[((0:(length(x)-1)) + (count %% length(x))) %% length(x) + 1]

    x <- 1:5

=> 2 3 4 5 1
    rotate(x, 2)
=> 3 4 5 1 2
    rotate(x, 3)
=> 4 5 1 2 3

One important thing about this is that the result of rotate(x)
is a permutation of x; it has the same number of elements, the same
elements, only the order is changed.

The reason I don't see _how_ to use embed() to do this is that the
result of embed is a matrix (but I want a vector) which has fewer rows
that the original (but I want the same number of elements).

    embed(x, n) => a matrix with length(x)-n+1 rows and n columns.

In the polygon application, embed(xs, 2) is very close to what's needed.
If the input to the polygon functions had the first element repeated at
the end, it would be just right:
b =>  b a
c     c b
a     a c
The problem is that the input isn't a b c a, it's a b c.

Let's not take up too many people's time with this, OK?
I hoped there might be a built-in function I had missed; apparently
there isn't.  End of story.

More information about the R-help mailing list