[英]Adding output vector as new rows of matrix in R
I have this code to test a for loop to generate multiple sample values of a given pdf: 我有此代码来测试for循环,以生成给定pdf的多个样本值:
library(EnvStats)
mvfy <- matrix(, nrow = 0, ncol = 1)
for (i in 1:1 ) {
meanmean = 400
sdmean = 5
lsup <- 1 - pnorm(420, mean = meanmean, sd = sdmean)
linf <- pnorm(380, mean = meanmean, sd = sdmean)
meanfy <- simulateVector(2, distribution = "norm",
param.list = list(mean = meanmean, sd = sdmean), #seed = i,
sort = FALSE, left.tail.cutoff = linf, right.tail.cutoff = lsup, sample.method = "LHS")
meansd = 10
sdsd = 5
lsup <- 1 - pnorm(20, mean = meansd, sd = sdsd)
linf <- pnorm(1, mean = meansd, sd = sdsd)
sdfy <- simulateVector(2, distribution = "norm",
param.list = list(mean = meansd, sd = sdsd), #seed = i,
sort = FALSE, left.tail.cutoff = linf, right.tail.cutoff = lsup, sample.method = "LHS")
lsup <- 1 - pnorm(480, mean = meanfy[1], sd = sdfy[1])
linf <- pnorm(320, mean = meanfy[1], sd = sdfy[1])
vfy <- simulateVector(100, distribution = "norm",
param.list = list(mean = meanfy[1], sd = sdfy[1]), #seed = i,
sort = FALSE, left.tail.cutoff = ifelse(linf == 0, .Machine$double.eps, linf),
right.tail.cutoff = ifelse(lsup == 0, .Machine$double.eps, lsup), sample.method = "LHS")
mvfy <- rbind(mvfy,vfy)
}
mvfy
When I run this, mvfy returns as: 当我运行此命令时,mvfy返回为:
row.names V1
1 NA
2 vfy 395.2071
but vfy is: 但是vfy是:
x
1 395.2071
2 391.8786
3 412.7885
4 402.2860
5 398.0058
6 400.4628
7 401.5104
8 384.1335
9 393.5518
10 394.5893
11 400.7124
12 410.1492
13 403.1595
14 392.8852
15 398.8437
16 396.3097
17 404.3873
18 401.7541
19 407.9139
20 409.4949
21 411.9881
22 396.6371
23 405.5770
24 406.8664
25 401.2580
26 406.3994
27 405.9094
28 400.1311
29 404.1403
30 403.7265
31 393.2893
32 393.9921
33 404.2963
34 406.1437
35 403.5337
36 407.7142
37 399.5886
38 392.4123
39 403.0684
40 402.1418
41 408.2856
42 398.3144
43 415.5288
44 399.1873
45 396.9163
46 399.7843
47 395.8027
48 407.0810
49 411.4461
50 401.6290
51 397.1390
52 404.7718
53 389.5800
54 397.2889
55 400.1586
56 390.4676
57 402.5914
58 395.5590
59 400.8974
60 394.9497
61 405.1056
62 396.8843
63 399.8768
64 405.2884
65 407.4649
66 408.9783
67 398.1736
68 406.7055
69 397.5084
70 402.8599
71 402.4824
72 392.2519
73 405.8249
74 397.7427
75 386.7654
76 401.9061
77 410.4451
78 401.1301
79 394.2435
80 409.1555
81 419.4111
82 403.3644
83 404.7034
84 398.5754
85 404.9945
86 411.0002
87 399.3490
88 394.6599
89 396.1322
90 398.8645
91 391.2988
92 403.8793
93 389.1929
94 395.9191
95 401.0488
96 400.4681
97 399.4857
98 402.7026
99 398.4219
100 408.5416
I get this warning: In rbind(mvfy, vfy) : number of columns of result is not a multiple of vector length (arg 2) 我收到此警告:在rbind(mvfy,vfy)中:结果的列数不是向量长度的倍数(arg 2)
I'd like to add vfy as rows of mvfy. 我想将vfy添加为mvfy的行。 I've tried some variants but I didn't succeed.
我尝试了一些变体,但没有成功。 Thanks
谢谢
In your example, mvfy
is a matrix and vfy
is just a simple vector. 在您的示例中,
mvfy
是一个矩阵,而vfy
只是一个简单的向量。 You cannot rbind()
a matrix and a vector in the way you expect. 您不能以您期望的方式
rbind()
矩阵和向量。 Observe that 观察一下
rbind(matrix(1:3, ncol=1), 4:6)
behaves the same way and returns an error. 行为相同,并返回错误。 You will either want to make
mvfy
and vfy
both numeric vectors, or both matricies. 您可能想同时制作
mvfy
和vfy
两个数值向量,或同时vfy
两个vfy
。 For the latter 对于后者
mvfy <- rbind(mvfy, matrix(vfy, ncol=1))
should work. 应该管用。 For the former, you'll want to initialize
mvfy
with 对于前者,您需要使用以下命令初始化
mvfy
mvfy <- numeric(0)
and add values with 并添加值
mvfy <- c(mvfy, vfy)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.