R for循环:对每个循环执行迭代
R for loop: perform iteration for every loop
我正在尝试 运行 在 R 中进行模拟,但是我在编写正确的 for 循环时遇到了问题。
我尝试执行的迭代是
i=1
distance<-NULL
for(i in 1:48)
{
sample<-coordinates[sample(.N, i)]
meand = (dist(cbind(sample$x,sample$y)))
ppp<-sample
table<-as.matrix(dist(ppp))
table[table == 0] <- 1000
maxmin<-apply(table, 1, FUN=min)
distance.1<-mean(maxmin)
distance<-rbind(distance,distance.1)
}
结果给出了一个 48 行的结果数据框,其中 i = 1:48
我想做的是 运行 for 循环中每个 i 大约 1000 次迭代。然后我想存储1000个结果的平均值,并为每个i存储它们。
我认为 replicate() 函数可能是解决方案,但我在使用它们时遇到了问题。
所以预期的输出有点
i=1 a (average of 1000 iteration)
i=2 b (average of 1000 iteration)
i=3 c (average of 1000 iteration)
.
.
.
i=48 d (average of 1000 iteration)
我应该如何重写我的代码以执行快速迭代?非常感谢您的帮助。
编辑
dput(coordinates)
structure(list(x = c(0.24, 0.72, 1.2, 3.675, 4.155, 4.635, 5.115,
5.595, 6.075, 8.55, 9.03, 9.51, 9.99, 10.47, 10.95, 13.425, 13.905,
14.385, 14.865, 15.345, 15.825, 18.3, 18.78, 19.26, 19.26, 18.78,
18.3, 15.825, 15.345, 14.865, 14.385, 13.905, 13.425, 10.95,
10.47, 9.99, 9.51, 9.03, 8.55, 6.075, 5.595, 5.115, 4.635, 4.155,
3.675, 1.2, 0.72, 0.24), y = c(0.24, 0.24, 0.24, 0.24, 0.24,
0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24,
0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 2.88, 2.88, 2.88,
2.88, 2.88, 2.88, 2.88, 2.88, 2.88, 2.88, 2.88, 2.88, 2.88, 2.88,
2.88, 2.88, 2.88, 2.88, 2.88, 2.88, 2.88, 2.88, 2.88, 2.88)), row.names = c(NA,
-48L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000027c2a7f1ef0>)
如果我理解正确你的问题,apply
函数可能会很好地解决你的问题。下面,我只是嵌套了一个 sapply
来在每个 i.
中进行 1000 次额外的重复
sapply(1:48, function(i){
mean(sapply(1:1000, function(x){
sample<-coordinates[sample(.N, i)]
meand = (dist(cbind(sample$x,sample$y)))
ppp<-sample
table<-as.matrix(dist(ppp))
table[table == 0] <- 1000
maxmin<-apply(table, 1, FUN=min)
mean(maxmin)
}))
})
如果有您的数据样本,我会更轻松。祝你好运!
我正在尝试 运行 在 R 中进行模拟,但是我在编写正确的 for 循环时遇到了问题。
我尝试执行的迭代是
i=1
distance<-NULL
for(i in 1:48)
{
sample<-coordinates[sample(.N, i)]
meand = (dist(cbind(sample$x,sample$y)))
ppp<-sample
table<-as.matrix(dist(ppp))
table[table == 0] <- 1000
maxmin<-apply(table, 1, FUN=min)
distance.1<-mean(maxmin)
distance<-rbind(distance,distance.1)
}
结果给出了一个 48 行的结果数据框,其中 i = 1:48
我想做的是 运行 for 循环中每个 i 大约 1000 次迭代。然后我想存储1000个结果的平均值,并为每个i存储它们。
我认为 replicate() 函数可能是解决方案,但我在使用它们时遇到了问题。
所以预期的输出有点
i=1 a (average of 1000 iteration)
i=2 b (average of 1000 iteration)
i=3 c (average of 1000 iteration)
.
.
.
i=48 d (average of 1000 iteration)
我应该如何重写我的代码以执行快速迭代?非常感谢您的帮助。
编辑
dput(coordinates)
structure(list(x = c(0.24, 0.72, 1.2, 3.675, 4.155, 4.635, 5.115,
5.595, 6.075, 8.55, 9.03, 9.51, 9.99, 10.47, 10.95, 13.425, 13.905,
14.385, 14.865, 15.345, 15.825, 18.3, 18.78, 19.26, 19.26, 18.78,
18.3, 15.825, 15.345, 14.865, 14.385, 13.905, 13.425, 10.95,
10.47, 9.99, 9.51, 9.03, 8.55, 6.075, 5.595, 5.115, 4.635, 4.155,
3.675, 1.2, 0.72, 0.24), y = c(0.24, 0.24, 0.24, 0.24, 0.24,
0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24,
0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 2.88, 2.88, 2.88,
2.88, 2.88, 2.88, 2.88, 2.88, 2.88, 2.88, 2.88, 2.88, 2.88, 2.88,
2.88, 2.88, 2.88, 2.88, 2.88, 2.88, 2.88, 2.88, 2.88, 2.88)), row.names = c(NA,
-48L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000027c2a7f1ef0>)
如果我理解正确你的问题,apply
函数可能会很好地解决你的问题。下面,我只是嵌套了一个 sapply
来在每个 i.
sapply(1:48, function(i){
mean(sapply(1:1000, function(x){
sample<-coordinates[sample(.N, i)]
meand = (dist(cbind(sample$x,sample$y)))
ppp<-sample
table<-as.matrix(dist(ppp))
table[table == 0] <- 1000
maxmin<-apply(table, 1, FUN=min)
mean(maxmin)
}))
})
如果有您的数据样本,我会更轻松。祝你好运!