Create a dataframe with random numbers in each column

后端 未结 2 1849
余生分开走
余生分开走 2020-12-09 16:19

I have this simulation of 1000 random numbers:

a <-sample(0:1, 1000, rep = TRUE)

What I want is a data frame of ten columns, where the v

2条回答
  •  失恋的感觉
    2020-12-09 16:48

    You are looking for replicate:

    data.frame(replicate(10,sample(0:1,1000,rep=TRUE)))
    

    These are the top few rows:

      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
    

    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.

提交回复
热议问题