创建一个在每一列中具有随机数的数据框 [英] Create a dataframe with random numbers in each column
本文介绍了创建一个在每一列中具有随机数的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我模拟了1000个随机数:
I have this simulation of 1000 random numbers:
a <-sample(0:1, 1000, rep = TRUE)
我想要的是一个十列的数据帧,其中每一列的值都像a一样生成.
What I want is a data frame of ten columns, where the values of each column are generated like a.
例如:
id Column 1 Column2 .........Column 10
1 1 1
2 0 1
3 1
0
0
.
.
1000 1 1
推荐答案
您在寻找replicate
:
data.frame(replicate(10,sample(0:1,1000,rep=TRUE)))
以下是前几行:
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1 1 1 0 1 0 0 1 1 1 0
2 0 0 0 1 0 1 0 0 1 0
3 0 1 1 1 1 0 1 1 1 1
4 0 0 0 1 1 1 1 1 1 0
5 1 0 1 0 1 1 0 1 1 0
6 0 1 1 1 1 1 0 1 1 1
如果执行相同的命令而不将其包装在data.frame()
中,则将有一个矩阵.矩阵的处理速度更快,因此您可能需要调查它们是否适合您的问题.
If you do the same command without wrapping it in data.frame()
, you will have a matrix. Matrices are faster to work with, so you might want to investigate whether they are suitable for your problem.
这篇关于创建一个在每一列中具有随机数的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文