在R中不使用foreach循环保存文件

bnl4lu3b  于 2023-04-18  发布在  其他
关注(0)|答案(2)|浏览(116)

我在将与“abc”相关的数据保存到名为“myResults.rds”的文件中时遇到了困难。我的代码很复杂,在foreach循环中涉及多个操作。我希望独占地保存我存储在名为“abc”的列表中的特定结果。下面是代码的说明:

library(foreach)
library(doParallel)

# create a cluster with 6 workers
cl <- makeCluster(6)
registerDoParallel(cl)

# define a local function
generate<-function(p){
  MIN <- (1-p)*100
  MAX <- (1+p)*100
  
  print(paste(MIN,MAX))
  mat <- matrix(nrow=1,ncol=4)
  
  for (i in seq(1,11,1)){ 
    a <- round(runif(1,MIN,MAX),2)
    b <- round(runif(1,MIN,MAX),2)
    c <- round(runif(1,MIN,MAX),2)
    d <- round(runif(1,MIN,MAX),2)
    mat <- rbind(mat,c(a,b,c,d))
  }
  return(mat[2:length(mat[,1]), ])
}

# run the loop with foreach and call the local function in parallel
abc<-list()
results <- foreach(i = 1:3) %dopar% {
  lstData=generate(0.05)
  abc[[i]]=lstData
  # main code goes here for analysis ......
  # Just added two simple operations for cheeking
  addNum <- 1+2+3
  subNum <- 1-2-3
}

saveRDS(abc, file = "myResults.rds")
stopCluster(cl)
w80xi6nr

w80xi6nr1#

以下是可以考虑的另一种方法:

library(foreach)
library(doParallel)

# create a cluster with 6 workers
cl <- makeCluster(6)
registerDoParallel(cl)

# define a local function
generate<-function(p){
  MIN <- (1-p)*100
  MAX <- (1+p)*100
  
  print(paste(MIN,MAX))
  mat <- matrix(nrow=1,ncol=4)
  
  for (i in seq(1,11,1)){ 
    a <- round(runif(1,MIN,MAX),2)
    b <- round(runif(1,MIN,MAX),2)
    c <- round(runif(1,MIN,MAX),2)
    d <- round(runif(1,MIN,MAX),2)
    mat <- rbind(mat,c(a,b,c,d))
  }
  return(mat[2:length(mat[,1]), ])
}

# run the loop with foreach and call the local function in parallel
abc <- foreach(i = 1:3) %dopar% {
  lstData <- generate(0.05)
  lstData
}

saveRDS(abc, file = "myResults.rds")
stopCluster(cl)

abc
[[1]]
        [,1]   [,2]   [,3]   [,4]
 [1,] 104.34  98.19  96.11 103.58
 [2,] 101.27 103.05 100.01 104.07
 [3,]  95.80  96.97 104.41 104.56
 [4,]  98.17  98.74  96.02  95.49
 [5,] 101.64 101.55 104.62 100.28
 [6,] 100.49 101.97  95.82  96.43
 [7,] 102.46  98.69 101.47 101.01
 [8,]  96.48  95.38  95.80  95.14
 [9,] 101.41 104.61 103.14  99.86
[10,] 104.56  97.43 100.10  98.58
[11,]  97.95 103.36 103.63  98.74

[[2]]
        [,1]   [,2]   [,3]   [,4]
 [1,]  95.78 103.40 103.07 103.83
 [2,] 100.48 104.37 100.59 103.10
 [3,]  99.93 101.82 100.00  98.57
 [4,]  97.94  97.77 101.50 103.82
 [5,]  98.82  96.73  99.26 104.16
 [6,]  97.58 104.66  99.21  99.95
 [7,]  99.02  97.37  98.06 100.84
 [8,]  97.04 104.66 102.65 101.49
 [9,]  97.67  97.18 100.35  97.41
[10,]  95.84 101.47  96.80  95.71
[11,]  97.12 104.15 101.98 100.59

[[3]]
        [,1]   [,2]   [,3]   [,4]
 [1,]  95.87 104.25 101.65  96.65
 [2,] 100.64  96.49  98.29 104.78
 [3,]  98.21  98.23  95.01  98.40
 [4,]  96.70 100.93 104.14  96.12
 [5,]  97.64  95.97  96.90  96.59
 [6,] 104.69 102.72 100.10 104.98
 [7,] 101.31  95.62  98.23 105.00
 [8,]  95.52  95.29 103.74  99.05
 [9,]  95.63  95.15 104.47  96.79
[10,] 101.12  98.20  97.86  97.84
[11,] 102.02 104.59 103.97  96.12
zfciruhq

zfciruhq2#

您可以使用以下方法:

library(parallel)
library(doParallel)

cl <- makeCluster(6)
registerDoParallel(cl)

fn_Par <- function(i)
{
  generate <- function(p)
  {
    MIN <- (1 - p) * 100
    MAX <- (1 + p) * 100
    
    print(paste(MIN,MAX))
    mat <- matrix(nrow = 1, ncol = 4)
    
    for(i in seq(1,11,1))
    { 
      a <- round(runif(1,MIN,MAX),2)
      b <- round(runif(1,MIN,MAX),2)
      c <- round(runif(1,MIN,MAX),2)
      d <- round(runif(1,MIN,MAX),2)
      mat <- rbind(mat,c(a,b,c,d))
    }
    
    return(mat[2:length(mat[,1]), ])
  }
  
  lstData <- generate(0.05)
  return(lstData)
}

abc <- lapply(X = 1 : 6, FUN = fn_Par)

saveRDS(abc, file = "myResults.rds")
stopCluster(cl)

abc
[[1]]
        [,1]   [,2]   [,3]   [,4]
 [1,] 101.72  97.27 104.22 102.61
 [2,] 102.18 101.19  96.69 104.42
 [3,]  96.05  99.53 104.94 102.48
 [4,]  95.15  96.34 103.93 104.43
 [5,]  96.88  98.15 100.54  96.18
 [6,]  99.08  95.40  99.32 100.73
 [7,]  97.82 100.05  98.71 101.64
 [8,] 100.27  96.29 103.34 102.24
 [9,] 102.85 103.86  95.51  96.03
[10,]  99.52 101.75 102.96  99.70
[11,] 104.38 100.80 101.38  99.80

[[2]]
        [,1]   [,2]   [,3]   [,4]
 [1,]  95.88  98.01 103.10  98.86
 [2,]  96.98  96.73 103.24 104.60
 [3,]  96.42  98.31  99.72 104.68
 [4,] 100.04  97.56  97.43 102.83
 [5,]  96.93 101.84  99.57 100.67
 [6,]  95.42  97.44  99.45  99.94
 [7,] 103.63 101.71  96.98 103.82
 [8,]  95.01  95.11  98.28  97.11
 [9,] 100.95  96.04 103.39  96.58
[10,]  99.84 102.48  96.60  96.31
[11,] 102.62 100.50 100.91 100.65

[[3]]
        [,1]   [,2]   [,3]   [,4]
 [1,] 102.37 102.34  95.97 103.03
 [2,] 100.54  98.33 104.42 104.64
 [3,]  97.60 102.36 101.94  97.25
 [4,] 102.52  99.55 104.26  98.54
 [5,] 103.29 100.85 102.18  96.17
 [6,]  95.08  97.56 100.73  97.45
 [7,] 100.04 101.55  99.67 103.31
 [8,]  98.09  96.42 102.55 102.45
 [9,]  98.52 101.20 104.02 100.16
[10,]  95.89 100.59  97.97 104.15
[11,]  96.12  99.26  99.89 100.45

[[4]]
        [,1]   [,2]   [,3]   [,4]
 [1,]  99.38  98.95  95.56 103.59
 [2,]  99.97 104.21 102.19 104.75
 [3,] 101.16  95.69 104.12  96.26
 [4,] 100.94 101.84  95.50 104.95
 [5,] 102.55 102.81  98.66  99.05
 [6,] 104.79 102.51 100.28  96.59
 [7,]  98.97  96.06  97.92 102.56
 [8,] 101.23  96.02 104.79  98.12
 [9,]  98.74 104.24  99.33 102.79
[10,]  95.75 103.19 101.87 104.19
[11,] 101.44  99.96 101.95 104.19

[[5]]
        [,1]   [,2]   [,3]   [,4]
 [1,] 101.06 100.26  96.95 102.47
 [2,] 100.19  98.53  99.49  95.46
 [3,]  99.75 102.08  97.76 100.41
 [4,] 101.12  98.33 104.19 104.25
 [5,]  98.98  97.59  95.70 100.45
 [6,] 104.97  96.87  99.70  97.44
 [7,] 103.62  97.90 104.00  95.84
 [8,] 103.94 103.35  95.60  95.32
 [9,]  97.81  95.08  99.76  97.44
[10,] 101.91 101.12 103.03  99.74
[11,] 101.87 102.89 100.91  99.22

[[6]]
        [,1]   [,2]   [,3]   [,4]
 [1,]  96.88 100.18 101.39 100.79
 [2,]  96.53 101.83 104.07 100.90
 [3,] 101.19  97.36 101.40 102.42
 [4,] 100.79  97.46 103.18 104.70
 [5,]  97.75  99.17  96.50 100.26
 [6,] 102.45  99.97 104.06  98.23
 [7,] 104.18 104.34  97.92 103.75
 [8,]  97.78  95.52  96.67 100.44
 [9,] 103.15  99.47 102.12 102.68
[10,] 101.45 100.71 101.90 101.54
[11,]  99.36  99.74  96.36  96.28

相关问题