在R中添加输出向量作为矩阵的新行

时间:2014-09-01 14:35:25

标签: r matrix row

我有这个代码来测试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

当我运行它时,mvfy返回为:

    row.names   V1
1              NA
2   vfy 395.2071

但是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

我收到此警告: 在rbind(mvfy,vfy):   结果列数不是向量长度的倍数(arg 2)

我想将vfy添加为mvfy行。我尝试了一些变种,但我没有成功。感谢

1 个答案:

答案 0 :(得分:1)

在您的示例中,mvfy是一个矩阵,vfy只是一个简单的向量。你不能以你期望的方式rbind()矩阵和向量。观察

rbind(matrix(1:3, ncol=1), 4:6)

行为相同并返回错误。您要么想要mvfyvfy两个数字向量,要么两个matricies。对于后者

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

应该有效。对于前者,您需要使用

初始化mvfy
mvfy <- numeric(0)

并使用

添加值
mvfy <- c(mvfy, vfy)