向量级联函数结果上的有害排序行为 [英] Unwanted sorted behavior on result of vector-concatenation function

查看:107
本文介绍了向量级联函数结果上的有害排序行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应用了一个简单的匿名函数以按序列1:5返回c(x,x + 5)

I apply a simple anonymous function to return c(x,x+5) on the sequence 1:5

我希望看到c(1,6,2,7,3,8,4,9,5,10)(子结果的串联),但是结果向量被不必要地排序了.该怎么做,我该如何预防?

I expect to see c(1,6,2,7,3,8,4,9,5,10) (the concatenation of the subresults) but instead the result vector is unwantedly sorted. What is doing that and how do I prevent it?

> (function(x) c(x,x+5)) (1:5)
 [1]  1  2  3  4  5  6  7  8  9 10

但是将函数应用于每个单独的参数是正确的:

However applying the function on each individual argument is right:

> (function(x) c(x,x+5)) (1)
[1] 1 6
> (function(x) c(x,x+5)) (2)
[1] 2 7
...
> (function(x) c(x,x+5)) (5)
[1]  5 10

推荐答案

您可以尝试以此欺骗操作顺序:

You could try this to spoof the order of operations:

foo<-function(x) {   
      bar<-cbind(x,x+5)  
      as.vector(t(bar))

}
foo(1:5)

或以一种行格式:

(function(x) as.vector(t(cbind(x,x+5)))) (1:5)

这篇关于向量级联函数结果上的有害排序行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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