简体   繁体   中英

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:

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:

    row.names   V1
1              NA
2   vfy 395.2071

but vfy is:

    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)

I'd like to add vfy as rows of 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. You cannot rbind() a matrix and a vector in the way you expect. 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. For the latter

mvfy <- rbind(mvfy, matrix(vfy, ncol=1))

should work. For the former, you'll want to initialize mvfy with

mvfy <- numeric(0)

and add values with

mvfy <- c(mvfy, vfy)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM