如何使用r获得数据帧中嵌套循环的输出?以及如何设置时间循环?

问题描述 投票:0回答:1

我是r的新手,并且从事布尔建模-我的嵌套循环以0和1给出了50个rtk值的输出,如何将该输出存储在数据帧中,cbind无法正常工作。请帮忙。2.另外,我想引入一个时间循环,即如果j = 5秒,我希望rtk将i0中的accoding值更改为j = 1至3,而时间j = 4,5秒,我希望rtk将accoding更改为nwki i0中的nwk值

遵循代码

library (BoolNet)#make sure lib is installed
library(igraph)
setwd("C:/Users/neha gopal/Desktop/IISC-mkj/r for boolean")#"

nwki<- loadNetwork("rtkufi.txt",bodySeparator =",",lowercaseGenes = FALSE)#"
nwk<- loadNetwork("rtkuf.txt",bodySeparator =",",lowercaseGenes = FALSE)#"
inc<-c(1,0,0,0,0,0,0,0,0,0)#vector
stF<- stateTransition(network=nwki,state= inc,type = "asynchronous")
stF
#loops working
rtk_values <-rep(0,50)# empty vector for total no of rtk  output values 
rtk_values
ts<- 10
for (i in 1:ts)# trajectories i=10
{
  i0 <- inc
  rtk_values <-rep(0,50)
  rtk <- i0[2]


tr<-5 #time j=5


for (j in 1:tr)
{


  i0 <- stateTransition(network=nwki,state= i0,type = "asynchronous")
  rtk <- i0[2]
  print (paste0( ",Im in J and my j is ", j, " and my i is", i,"rtk is" , rtk ))

}

}

#table  NOT working


rtk_values[j]= rtk
rtk_table <- cbind.data.frame( rtk_values, rtk)
r boolean modeling
1个回答
0
投票

提出问题的最佳方法是提供一个可复制的示例。如果您的问题是如何从for循环中存储变量,这将是一种实现方法:

result <- list() # initalize an empty list

for (i in 1:10){
  result[[i]] <- i # populate list with every loop
}

同一系统可用于嵌套循环:

result <- list()
for (i in 1:10){
  result_nested <- list() # initialize an empty list within the loop
  for (nested in 1:5){
    result_nested[[nested]] <- nested # populate the results from the nested loop. 
  }
  result[[i]] <- result_nested # populate the result from the nested loop to the parent loop
}
© www.soinside.com 2019 - 2024. All rights reserved.