-2

I have a matrix(NA, 10000,6),and I'm trying to replace all the NA with a random data set.How should I do it?

Thanx

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187

1 Answers1

0

You might try using replicate

N <- 10000
M <- 6
random_start = 0
random_end = 1000
matrix(replicate(10,sample(random_start:random_end,1000,rep=TRUE)), 10000,6)

This will give you an NxM matrix of random values between random_start and random_end

If you want floating point you can also try

matrix( rnorm(N*M,mean=0,sd=1), N, M)

With these I get the following

head(matrix(replicate(10,sample(random_start:random_end,1000,rep=TRUE)), N,M))
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]  571  571  571  571  571  571
[2,]  673  673  673  673  673  673
[3,]  718  718  718  718  718  718
[4,]  978  978  978  978  978  978
[5,]  279  279  279  279  279  279
[6,]  592  592  592  592  592  592

head(matrix( rnorm(N*M,mean=0,sd=1), N, M) )
       [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
[1,]  0.9792492  0.1608276  0.3642330  0.3181538  0.7867654 -0.9961405
[2,]  1.2093093  0.3340030  1.4143809 -1.1286336 -0.7070079 -1.6437918
[3,] -1.2597694 -0.1130619  1.3026433 -0.5397444  0.5431179  0.2270258
[4,] -0.7374622  0.6025627  1.6510483  0.3930691  1.0022722 -0.7458319
[5,]  0.8891398 -0.5967384 -0.2739391  1.3559169  0.7388975  0.9393390
[6,] -1.2376336 -0.3224949  1.1807379 -0.0701154  2.0532332  0.7610894

R - Create a data frame with random numbers in each column

Generate matrix with iid normal random variables using R

Community
  • 1
  • 1
Newyork167
  • 494
  • 5
  • 9