我有两个矩阵,“dm”和“schedule”:
dm<-matrix(c(5,3,6,9,3,8),2,3)
dm
[,1] [,2] [,3]
[1,] 5 6 3
[2,] 3 9 8
schedule<-matrix(c(6.3,5.3,15.2,9.7, 8.1,21.0),2,3)
schedule
[,1] [,2] [,3]
[1,] 6.3 15.2 8.1
[2,] 5.3 9.7 21.0
现在我想使用两个矩阵进行for循环:
library(plyr)
for (i in 1:3){
for (q in 1:2){
a<-data.frame(t(sort(runif(dm[q,i], 0, schedule[q,i]))))
l<-list(a)
p<-do.call(rbind.fill, l)
print(p)
}
}
获得的结果是:
X1 X2 X3 X4 X5
1 1.1483 2.746123 3.846865 5.20824 6.028914
X1 X2 X3
1 0.09545895 0.6899679 3.311584
X1 X2 X3 X4 X5 X6
1 0.0373203 1.213415 3.450417 7.803174 11.28925 15.17946
X1 X2 X3 X4 X5 X6 X7 X8 X9
1 0.9628973 1.922808 3.734731 4.45964 4.60855 5.756587 6.259947 6.707502 8.352062
X1 X2 X3
1 3.087143 5.518346 6.078802
X1 X2 X3 X4 X5 X6 X7 X8
1 3.588025 6.493965 6.868749 7.912049 9.164231 9.993325 15.34702 19.83849
我希望结果看起来像这样,然后存储结果以供进一步分析。
X1 X2 X3 X4 X5 X6 X7 X8 X9
1.1483 2.746123 3.846865 5.20824 6.028914 NA NA NA NA
0.09545895 0.6899679 3.311584 NA NA NA NA NA NA
0.0373203 1.213415 3.450417 7.803174 11.28925 15.17946 NA NA NA
0.9628973 1.922808 3.734731 4.45964 4.60855 5.756587 6.259947 6.707502 8.352062
3.087143 5.518346 6.078802 NA NA NA NA NA NA
3.588025 6.493965 6.868749 7.912049 9.164231 9.993325 15.34702 19.83849 NA
我一直在努力,但我无法解决这个问题。提前谢谢。
# **UPDATES**
#
我申请代码后:
set.seed(567)
library(plyr)
for (i in 1:3){
for (q in 1:2){
w<-do.call(rbind.fill,lapply(1:3,function(i)
do.call(rbind.fill,lapply(1:2, function(q)
data.frame(t(sort(runif(dm[q,i],0, schedule[q,i]))))))))
print(w)
}
}
我得到了6组结果:
X1 X2 X3 X4 X5 X6 X7 X8 X9
1 1.6432907 3.0827761 3.9863873 4.689270 5.515190 NA NA NA NA
2 0.3970765 1.2801990 2.7206058 NA NA NA NA NA NA
3 4.1608265 5.1831060 5.3924656 8.139031 8.928247 8.978007 NA NA NA
4 0.1635540 0.2541877 0.6130778 1.236997 2.179649 2.831193 5.068474 5.081584 7.246205
5 0.1284468 3.1131624 6.7266701 NA NA NA NA NA NA
6 0.5639268 8.2686410 11.2430075 14.828371 17.526983 19.172721 19.464813 20.096544 NA
X1 X2 X3 X4 X5 X6 X7 X8 X9
1 1.82632052 4.262909 4.508882 5.806028 5.864306 NA NA NA NA
2 0.08290761 1.725326 4.665457 NA NA NA NA NA NA
3 1.83091637 3.697634 4.624553 4.877007 6.616031 11.094916 NA NA NA
4 1.39282538 3.490440 3.498343 4.051373 6.513588 7.452566 7.95394 8.239656 8.248208
5 1.60726975 2.072848 5.048690 NA NA NA NA NA NA
6 3.15490789 3.950510 10.608165 10.865790 11.574824 14.850330 14.97088 19.527449 NA
X1 X2 X3 X4 X5 X6 X7 X8 X9
1 0.5606442 0.9977634 3.161933 4.026845 5.793865 NA NA NA NA
2 3.5053743 4.3333727 4.379345 NA NA NA NA NA NA
3 1.9297694 3.3119283 8.884994 10.295379 12.759272 14.756985 NA NA NA
4 1.6398505 2.0005113 3.588146 3.652551 3.952168 4.930368 5.321089 6.712174 6.996944
5 0.9400254 3.9303393 6.564849 NA NA NA NA NA NA
6 3.1807685 3.8952143 5.277726 6.373959 8.384240 11.821046 13.534297 18.934086 NA
X1 X2 X3 X4 X5 X6 X7 X8 X9
1 0.25905911 2.2612541 2.364979 3.344301 5.744027 NA NA NA NA
2 0.38830469 3.9903400 4.882548 NA NA NA NA NA NA
3 0.04137479 0.4478564 2.587154 5.421488 6.429712 14.736563 NA NA NA
4 1.29092839 2.2867913 3.645377 4.235872 4.448638 6.117092 7.037541 7.490561 7.670878
5 0.33135574 5.7413350 7.805096 NA NA NA NA NA NA
6 6.23169085 7.5107094 8.141378 9.606699 11.043875 13.606426 17.330823 20.372078 NA
X1 X2 X3 X4 X5 X6 X7 X8 X9
1 0.007934088 0.5045294 1.186808 2.990181 5.783975 NA NA NA NA
2 2.493238768 2.8420446 4.999854 NA NA NA NA NA NA
3 0.544283423 1.8838762 3.310545 4.060896 9.613281 12.354680 NA NA NA
4 1.837721876 1.8548055 3.816228 4.132309 5.269259 5.729938 7.471994 8.329342 8.550461
5 0.489360828 7.2928492 7.321177 NA NA NA NA NA NA
6 1.323669112 2.7321133 2.819985 4.743785 6.727880 13.338897 17.495578 19.840468 NA
X1 X2 X3 X4 X5 X6 X7 X8 X9
1 0.68498784 2.6040049 3.614236 4.069595 4.600384 NA NA NA NA
2 2.57882444 3.5311153 5.116656 NA NA NA NA NA NA
3 1.39984330 3.2426665 3.256548 10.461186 13.605143 14.513957 NA NA NA
4 0.08044902 0.1764063 1.771046 2.412206 3.734814 6.371065 7.352427 7.588615 9.392168
5 0.48088901 3.6171198 6.550506 NA NA NA NA NA NA
6 0.68160481 2.6673476 10.728082 11.572912 11.665851 12.580821 14.796373 19.908947 NA
当我使用“write.table”命令导出结果时,将“print(w)”替换为 “write.table(w,”file.csv“,sep =”,“,row.names = F)”,只有最后组的结果出现了 在“file.csv”中:(第6组结果)
X1 X2 X3 X4 X5 X6 X7 X8 X9
0.68498784 2.6040049 3.614236 4.069595 4.600384 NA NA NA NA
2.57882444 3.5311153 5.116656 NA NA NA NA NA NA
1.39984330 3.2426665 3.256548 10.461186 13.605143 14.513957 NA NA NA
0.08044902 0.1764063 1.771046 2.412206 3.734814 6.371065 7.352427 7.588615 9.392168
0.48088901 3.6171198 6.550506 NA NA NA NA NA NA
0.68160481 2.6673476 10.728082 11.572912 11.665851 12.580821 14.796373 19.908947 NA
实际上只有第一个结果集是我想要的,保存到“.csv”文件,并用于进一步分析(我的原始代码产生了第一组结果) : (第一组结果)
X1 X2 X3 X4 X5 X6 X7 X8 X9
1.6432907 3.0827761 3.9863873 4.689270 5.515190 NA NA NA NA
0.3970765 1.2801990 2.7206058 NA NA NA NA NA NA
4.1608265 5.1831060 5.3924656 8.139031 8.928247 8.978007 NA NA NA
0.1635540 0.2541877 0.6130778 1.236997 2.179649 2.831193 5.068474 5.081584 7.246205
0.1284468 3.1131624 6.7266701 NA NA NA NA NA NA
0.5639268 8.2686410 11.2430075 14.828371 17.526983 19.172721 19.464813 20.096544 NA
答案 0 :(得分:0)
res1 <- do.call(rbind.fill,
lapply(1:3, function(i)
do.call(rbind.fill,
lapply(1:2, function(q)
data.frame(t(sort(runif(dm[q, i], 0, schedule[q, i]))))))))
head(res1,3)
# X1 X2 X3 X4 X5 X6 X7 X8 X9
#1 0.3498861 2.969862 3.473222 3.851105 4.446656 NA NA NA NA
#2 0.2395686 2.477478 3.297952 NA NA NA NA NA NA
#3 0.1117403 0.422571 1.267864 10.168734 12.511853 14.32337 NA NA NA