使用R:将矩阵转换为向量序列以实现阶函数的可变方法 [英] Using R: casting a matrix as a sequence of vectors to implement a variadic approach for order function

查看:223
本文介绍了使用R:将矩阵转换为向量序列以实现阶函数的可变方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

order函数描述其在列表中的读取方式

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] ), ];

所以我有一个要根据其杂散列数分解的矩阵,但将该矩阵转换为序列 order 函数可以处理的向量。 不列出?也许 as.list

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?

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
> 




这里有一个...步骤,但是我们得到一个矩阵 vecs 看起来像这样:

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
> 

所以我尝试这样: vec2 = as.data.frame(vecs); class(vec2)= list; 基于另一篇文章(alfymbohm)如何将矩阵转换为R中的列向量列表?

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?

当前有效:

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

我想要的工作失败了。我用 vec2 来区分它。

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

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

它( order 函数)抛出以下错误:

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);

其中

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;
    }

没有用!引发相同的错误( order 函数):

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'


同样,可变参数当前不起作用,因为矩阵不是向量序列。根据订单的手册。

如何根据矩阵的数量将矩阵转换为向量序列列,这样 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?

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.

推荐答案

如果要将矩阵的列传递给 order ,就像您在调用 order(mat [, 1],mat [,2],mat [,3])等,那么这一行函数就可以实现:

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)))

首先使用一些模块化数学方法,将<< c> c 将矩阵列分成矢量列表,然后在结果上使用 do.call(order,...),其结果是传递每个列表元素(即每个向量)作为变量。

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.

这篇关于使用R:将矩阵转换为向量序列以实现阶函数的可变方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆