替换Rstudio中矩阵中的每一列

时间:2016-03-23 03:45:15

标签: r

我有一个矩阵(NA,10000,6),我试图用随机数据集替换所有NA。我该怎么做?

感谢名单

1 个答案:

答案 0 :(得分:0)

您可以尝试使用复制

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

这将为您提供random_start和random_end

之间随机值的NxM矩阵

如果你想要浮点数,你也可以尝试

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

有了这些,我得到以下

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