[Rd] problem with aperm? (PR#568)

Prof Brian D Ripley ripley@stats.ox.ac.uk
Tue, 13 Jun 2000 07:41:41 +0100 (BST)

On Tue, 13 Jun 2000 jim.lemon@uts.edu.au wrote:

> R version 1.0.1
> OS RedHat Linux 6.1
> In attempting to test for numeric vectors in a data frame, I tried:
> apply(dataframe,2,is.numeric)
> and found that it returned FALSE for all vectors whether they were
> numeric or not.  I tracked this to the fact that as.array() was
> converting the data frame to character vectors, and thought I could
> solve it by using array(), which preserved the mode of the columns. 
> However, I then got an error from aperm():

What? arrays are only of one mode. Try

sapply(dataframe, is.numeric)

for what you wanted.  For example,

sapply(Cars93, is.numeric)
      Manufacturer              Model               Type          Min.Price 
             FALSE              FALSE              FALSE               TRUE 
             Price          Max.Price           MPG.city        MPG.highway 
              TRUE               TRUE               TRUE               TRUE 

The help page for array says


       x: the array to be used.

and a dataframe is *not* an array (or a matrix).

What did you run to get the following?

> Error in aperm(X, c(s.call, s.ans)) : aperm: invalid second argument,
> must be a vector
> even though the second argument tested as a vector.  This happened both
> within the apply() function and calling aperm() separately, even if I
> explicitly used a numeric vector for the second argument.  I then had a
> look at the code for do_aperm(), but as I am unfamiliar with Scheme, had
> to give up after trying to work out what CADR and SEXP were.

Can you please tell us exactly what you did: there is no actual example
of a problem with aperm here!

The error message is too curt: it actually means `must be a vector of the
right length', and I am fairly sure that is your error here. The code

    if (!isVector(perm) || (length(perm) != length(dimsa)))
        error("aperm: invalid second argument, must be a vector");

is not too hard to understand without any knowledge of Scheme (and I have
none either: the internal R language is described in `Writing R

Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch