Search code examples
rcastingvariadic-functionsvariadic

Using R: casting a matrix as a sequence of vectors to implement a variadic approach for order function


The order function describes how it reads in its lists

?order

... 
a sequence of numeric, complex, character or logical vectors, all of the same length, or a classed R object.

-----------------------------------------------------

> order
function (..., na.last = TRUE, decreasing = FALSE, method = c("auto", 
    "shell", "radix")) 
{
    z <- list(...)
    decreasing <- as.logical(decreasing)
    if (length(z) == 1L && is.numeric(x <- z[[1L]]) && !is.object(x) && 
        length(x) > 0) {
        if (.Internal(sorted_fpass(x, decreasing, na.last))) 
            return(seq_along(x))
    }

Most people use order in a hacked, non-variadic form:

myData.sorted = myData[ order(-myData[,date.idx],-myData[,(1+date.idx)]), ];

I have written a function to make this form variadic:

        #########################################
        ## how I want it, doesn't work
        #fdf = sdf[order(vecs), ];

        #########################################
        ## non-variadic approach, does work
        fdf = sdf[order( vecs[,1],vecs[,2],vecs[,3] ), ];

So I have a matrix that I want to decompose based on its variadic number of columns, yet cast that matrix as a sequence of vectors that the order function can handle. unlist? maybe as.list?

How can I cast a matrix to be a sequence of vectors based on its number of columns?


Update

convertDateStringToFormat = function (strvec,format.out="%Y",format.in="%Y-%m-%d %H:%M:%S",numeric=TRUE)
    {
    p.obj = strptime(strvec, format=format.in);
    o.obj = strftime(p.obj, format=format.out);
    
    if(numeric) { as.numeric(o.obj); } else { o.obj; }
    }

library(datasets);
data(iris);
df = iris[1:10,];
df$date.strings = c("3/24/2010 18:33", "9/3/2009 17:28", "10/14/2009 11:40", "7/3/2015 11:16","11/18/2010 1:29","4/23/2011 0:08","10/6/2010 11:13","7/26/2009 13:23","4/9/2008 13:40","8/20/2008 11:32");
df$year = convertDateStringToFormat(df$date.strings,"%Y","%m/%d/%Y %H:%M");
df$week = convertDateStringToFormat(df$date.strings,"%W","%m/%d/%Y %H:%M");
df$day = convertDateStringToFormat(df$date.strings,"%j","%m/%d/%Y %H:%M");
df$date.strings = NULL;

> df
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species year week day
1           5.1         3.5          1.4         0.2  setosa 2010   12  83
2           4.9         3.0          1.4         0.2  setosa 2009   35 246
3           4.7         3.2          1.3         0.2  setosa 2009   41 287
4           4.6         3.1          1.5         0.2  setosa 2015   26 184
5           5.0         3.6          1.4         0.2  setosa 2010   46 322
6           5.4         3.9          1.7         0.4  setosa 2011   16 113
7           4.6         3.4          1.4         0.3  setosa 2010   40 279
8           5.0         3.4          1.5         0.2  setosa 2009   29 207
9           4.4         2.9          1.4         0.2  setosa 2008   14 100
10          4.9         3.1          1.5         0.1  setosa 2008   33 233
> 

There is a ... step here, but we get a matrix vecs that looks like this:

vecs = matrix(
            c(2010,2009,2009,2015,2010,2011,2010,2009,2008,2008,
            -12,-35,-41,-26,-46,-16,-40,-29,-14,-33,
            83,246,287,184,322,113,279,207,100,233),
            
    nrow=10,ncol=3,byrow=F);

> vecs
      [,1] [,2] [,3]
 [1,] 2010  -12   83
 [2,] 2009  -35  246
 [3,] 2009  -41  287
 [4,] 2015  -26  184
 [5,] 2010  -46  322
 [6,] 2011  -16  113
 [7,] 2010  -40  279
 [8,] 2009  -29  207
 [9,] 2008  -14  100
[10,] 2008  -33  233
> 

So I try this: vec2 = as.data.frame(vecs); class(vec2) = "list"; based on another post (alfymbohm) How to convert a matrix to a list of column-vectors in R?

Currently, this works:

df[order( vecs[,1],vecs[,2],vecs[,3] ), ];


   Sepal.Length Sepal.Width Petal.Length Petal.Width Species year week day
10          4.9         3.1          1.5         0.1  setosa 2008   33 233
9           4.4         2.9          1.4         0.2  setosa 2008   14 100
3           4.7         3.2          1.3         0.2  setosa 2009   41 287
2           4.9         3.0          1.4         0.2  setosa 2009   35 246
8           5.0         3.4          1.5         0.2  setosa 2009   29 207
5           5.0         3.6          1.4         0.2  setosa 2010   46 322
7           4.6         3.4          1.4         0.3  setosa 2010   40 279
1           5.1         3.5          1.4         0.2  setosa 2010   12  83
6           5.4         3.9          1.7         0.4  setosa 2011   16 113
4           4.6         3.1          1.5         0.2  setosa 2015   26 184

And what I want to work fails. I use vec2 to distinguish it.

vec2 = as.data.frame(vecs); class(vec2) = "list";
df[order(vec2), ];

It (the order function) throws the following error:

Error in order(vec2) : unimplemented type 'list' in 'orderVector1'

I see your approach as the cast-as-list idea I found elsewhere.

Ideally, I would want a function such as

vec2 = castMatrixToSequenceOfLists(vecs);

where

https://stackoverflow.com/questions/6819804/how-to-convert-a-matrix-to-a-list-of-column-vectors-in-r    
castMatrixToSequenceOfLists = function(mat)
    {
    list_length = ncol(mat);
    out_list = vector("list", list_length);
    for(i in 1:list_length)
        {
        out_list[[i]] = mat[,i]; # double brackets [[1]]
        }
    out_list;
    }

Did not work! Throws the same error (the order function):

vec2 = castMatrixToSequenceOfLists(vecs);
df[order(vec2), ];


Error in order(vec2) : unimplemented type 'list' in 'orderVector1'

Again, variadic doesn't currently work because the matrix is not a "sequence of vectors" according to the manual for order.

How do I cast a matrix as a sequence of vectors based on its number of columns so the order function will accept it?

Solution

mat_order <- function(x) do.call(order, split(x, (seq(x) - 1) %/% nrow(x)))

> df[mat_order(vecs),]
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species year week day
10          4.9         3.1          1.5         0.1  setosa 2008   33 233
9           4.4         2.9          1.4         0.2  setosa 2008   14 100
3           4.7         3.2          1.3         0.2  setosa 2009   41 287
2           4.9         3.0          1.4         0.2  setosa 2009   35 246
8           5.0         3.4          1.5         0.2  setosa 2009   29 207
5           5.0         3.6          1.4         0.2  setosa 2010   46 322
7           4.6         3.4          1.4         0.3  setosa 2010   40 279
1           5.1         3.5          1.4         0.2  setosa 2010   12  83
6           5.4         3.9          1.7         0.4  setosa 2011   16 113
4           4.6         3.1          1.5         0.2  setosa 2015   26 184

This works as expected in variadic form.


Solution

  • If you want to pass the columns of a matrix to order as if you were calling order(mat[,1], mat[,2], mat[,3]) etc, then this one line function achieves that:

    mat_order <- function(x) do.call(order, split(x, (seq(x) - 1) %/% nrow(x)))
    

    It first splits the matrix columns into a list of vectors using a little modular maths, then uses do.call(order, ...) on the result, which has the effect of passing each list element (i.e. each vector) as variadics.