如何将向量分成两列以创建用于随机分配的有序对
我正在尝试从 34 个受试者中生成随机对进行实验。受试者将被分配 ID 号 1-34。为了生成随机排序的数字(1-34),我使用了以下代码:
### Getting a vector of random ordered numbers 1-34###
pairs<-sample(1:34,34,replace=F)
pairs
[1] 16 22 8 13 4 25 18 12 17 5 6 31 29 27 30 23 2 14 9 24 34 21 11
3 1 28 33 20 32 26 19 10 15 7
我想做的是采用数字的随机排序并将向量的每个其他元素拆分为一列,以便获得以下有序对:
partner1 partner2
16 22
8 13
. .
. .
15 7
关于如何从向量到有序对的想法或想法?任何帮助或见解将不胜感激。
-托马斯
I am trying to generate random pairs from 34 subjects for an experiment. Subjects will be assigned ID #'s 1-34. To generate the random ordered numbers (1-34) I used the following code:
### Getting a vector of random ordered numbers 1-34###
pairs<-sample(1:34,34,replace=F)
pairs
[1] 16 22 8 13 4 25 18 12 17 5 6 31 29 27 30 23 2 14 9 24 34 21 11
3 1 28 33 20 32 26 19 10 15 7
What I would like to do is to take this random ordering of numbers and split every other element of the vector into a column so that I get the following ordered pairs:
partner1 partner2
16 22
8 13
. .
. .
15 7
Thoughts or ideas on how to go from the vector to the ordered pairs? Any help or insight would be much appreciated.
-Thomas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这可能很简单
,然后每一行都会给你一对。
编辑正确使用
matrix()
That could be as easy as
and each row then gives you the pair.
Edit Correct to use
matrix()
将向量拆分为矩阵并迭代结果的行和列。矩阵就像二维数组一样呈现。因此,您需要在此矩阵中设置为向量中奇数位置处的行元素中的第一个元素和向量中偶数位置处的行元素中的第二个元素。
var 向量 = 新字节[34]; //fill it
上面的代码没有经过测试,但是算法和想法很清楚。
Split your vector into matrix and iterate over rows and columns of result. Matrix is presented like 2 dimensional array. So you need to set in this matrix to the first element in row element at the odd position in vector and to the second element in row - even position in vector.
var vector = new byte[34]; //fill it
Code above not tested, but algorythm and idea is clear.
或者,如果您想要一个数据框:
alternately, if you want a data frame:
您还可以将上面的 Dirk 代码压缩为一行
You can also condense Dirk's code above into one line