仅在 R 中条件为真时打印

Print Only When Condition is True in R

这个解决方案

library(forecast)
SEED_vector <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19)
arima_order_results = data.frame()
for (my_seed in SEED_vector){
  set.seed(my_seed)
  ar1 <- arima.sim(n = 10, model=list(ar=0.2, order = c(1, 0, 0)), sd = 1)
  ar2 <- auto.arima(ar1, ic ="aicc")

  arima_order = arimaorder(ar2)
  arima_order = t(as.data.frame(arima_order))
  # Print the arima order.
  print(arima_order)
  # This line of code is just if you want to store the results in a dataframe
  arima_order_results = rbind(arima_order_results,arima_order)
}

我只想打印 arimaorder of (1, 0, 0) 而不是全部

我试过了,但出现错误:

SEED_vector <- 1:100
arima_order_results <- data.frame()
while(TRUE){for (my_seed in SEED_vector) {set.seed(my_seed)
    ar1 <- arima.sim(n = 10, model=list(ar=0.2, order = c(1, 0, 0)), sd = 1)
    ar2 <- auto.arima(ar1, ic ="aicc")
    if(all(arimaorder(ar2)==c(1,0,0))) print(arima_order_results)
    arima_order = arimaorder(ar2)
    arima_order = t(as.data.frame(arima_order))
    # Print the arima order.
    print(arima_order)
    # This line of code is just if yo uwant to store the results in a dataframe
    arima_order_results = rbind(arima_order_results,arima_order)

}}

我想要这样的东西:

#arima_order14 1 0 0 
#arima_order39 1 0 0 
#arima_order47 1 0 0 

也许,这有帮助

SEED_vector <- 1:200
arima_order_results <- data.frame()
flag <- TRUE
i <- 1
seed_out <- c()
while(flag){ 
 
     set.seed(SEED_vector[i])
     ar1 <- arima.sim(n = 10, model=list(ar=0.2, order = c(1, 0, 0)), sd = 1)
     ar2 <- auto.arima(ar1, ic ="aicc")
     if(all(arimaorder(ar2)==c(1,0,0))) {
     
        #print(arima_order_results)
        print(paste0('arimaorder', SEED_vector[i], ' ' , 
              paste(arimaorder(ar2), collapse=" ")))
         seed_out <- c(seed_out, SEED_vector[i])
        
        }
       
     arima_order = arimaorder(ar2)
     arima_order = t(as.data.frame(arima_order))
     
     
     arima_order_results = rbind(arima_order_results,arima_order)
     
     i <- i+1
     if(i == length(SEED_vector)) {
     
      flag <- FALSE
     }

 }

-输出

#[1] "arimaorder14 1 0 0"
#[1] "arimaorder152 1 0 0"
#[1] "arimaorder165 1 0 0"

现在,我们检查矢量

seed_out
#[1]  14 152 165

可以用write.tablecat

写入文件
cat(seed_out, file = 'seed_file.txt')