重复功能(或类似)

时间:2016-06-01 12:15:35

标签: r function

我正在编写一段代码,我需要多次应用(可能总共几百个)。我已经将很多东西整理成功能,这些功能已经让事情变得更加整洁了。但我最终还是会列出一长串列表,例如code(1)code(2)code(3)等等。

我尝试过使用序列命令和循环(严重),但最后我不得不放弃。有没有办法整理这些代码的重复? (如果需要,我可以粘贴所有修改过的函数,但它们应该是无关紧要的(我希望))。谢谢!

library(leaps)
library(grid)
library(gridBase)

source("J:/R/Functions/function_leapsSummary.R")
source("J:/R/Functions/function_plotregsubsetsMOD.R")

nbestfunc <- function(D, E, G){
  regsubsets(D~.,
             data=E,
             nbest=G)
}

setwd("J:/Academic papers/Dissertation journal paper/Version 3/New analysis/R code/Full data/SpringLIFE-ChalkRiver")

SpringLIFEChalkRiver <- read.csv("SpringLIFE-ChalkRiver.csv",header=T)
attach(SpringLIFEChalkRiver)

SPR_Q_PCA <- SpringLIFEChalkRiver[,c(3, 7, 12, 14, 16, 18)]

setwd("J:/R/Leaps")

##nbest
nb1 <- nbestfunc(SpringChalkLife, SPR_Q_PCA, G = 1)
nb2 <- nbestfunc(SpringChalkLife, SPR_Q_PCA, G = 2)
nb3 <- nbestfunc(SpringChalkLife, SPR_Q_PCA, G = 3)
nb4 <- nbestfunc(SpringChalkLife, SPR_Q_PCA, G = 4)
nb5 <- nbestfunc(SpringChalkLife, SPR_Q_PCA, G = 5)
nb6 <- nbestfunc(SpringChalkLife, SPR_Q_PCA, G = 6)
nb7 <- nbestfunc(SpringChalkLife, SPR_Q_PCA, G = 7)
nb8 <- nbestfunc(SpringChalkLife, SPR_Q_PCA, G = 8)
nb9 <- nbestfunc(SpringChalkLife, SPR_Q_PCA, G = 9)
nb10 <- nbestfunc(SpringChalkLife, SPR_Q_PCA, G = 10)

##df from captions and file names
NAMES <- cbind(c(
  "Spring_VPCA_nbest=1", 
  "Spring_VPCA_nbest=2", 
  "Spring_VPCA_nbest=3",
  "Spring_VPCA_nbest=4",
  "Spring_VPCA_nbest=5",
  "Spring_VPCA_nbest=6",
  "Spring_VPCA_nbest=7",
  "Spring_VPCA_nbest=8",
  "Spring_VPCA_nbest=9",
  "Spring_VPCA_nbest=10"),
  c(
    "J:/R/Leaps/Spring_VPCA_nbest=1.png",
    "J:/R/Leaps/Spring_VPCA_nbest=2.png",
    "J:/R/Leaps/Spring_VPCA_nbest=3.png",
    "J:/R/Leaps/Spring_VPCA_nbest=4.png",
    "J:/R/Leaps/Spring_VPCA_nbest=5.png",
    "J:/R/Leaps/Spring_VPCA_nbest=6.png",
    "J:/R/Leaps/Spring_VPCA_nbest=7.png",
    "J:/R/Leaps/Spring_VPCA_nbest=8.png",
    "J:/R/Leaps/Spring_VPCA_nbest=9.png",
    "J:/R/Leaps/Spring_VPCA_nbest=10.png"))

##calling function for each
leapsSummary(nb1, B = NAMES[1,1], C = NAMES[1,2])
dev.off()
leapsSummary(nb2, B = NAMES[2,1], C = NAMES[2,2])
dev.off()
leapsSummary(nb3, B = NAMES[3,1], C = NAMES[3,2])
dev.off()
leapsSummary(nb4, B = NAMES[4,1], C = NAMES[4,2])
dev.off()
leapsSummary(nb5, B = NAMES[5,1], C = NAMES[5,2])
dev.off()
leapsSummary(nb6, B = NAMES[6,1], C = NAMES[6,2])
dev.off()
leapsSummary(nb7, B = NAMES[7,1], C = NAMES[7,2])
dev.off()
leapsSummary(nb8, B = NAMES[8,1], C = NAMES[8,2])
dev.off()
leapsSummary(nb9, B = NAMES[9,1], C = NAMES[9,2])
dev.off()
leapsSummary(nb10, B = NAMES[10,1], C = NAMES[10,2])

str(sum_list)的输出

 List of 10

 $ : Named int 2
  ..- attr(*, "names")= chr "RStudioGD"
 $ : Named int 2
  ..- attr(*, "names")= chr "RStudioGD"
 $ : Named int 2
  ..- attr(*, "names")= chr "RStudioGD"
 $ : Named int 2
  ..- attr(*, "names")= chr "RStudioGD"
 $ : Named int 2
  ..- attr(*, "names")= chr "RStudioGD"
 $ : Named int 2
  ..- attr(*, "names")= chr "RStudioGD"
 $ : Named int 2
  ..- attr(*, "names")= chr "RStudioGD"
 $ : Named int 2
  ..- attr(*, "names")= chr "RStudioGD"
 $ : Named int 2
  ..- attr(*, "names")= chr "RStudioGD"
 $ : Named int 2
  ..- attr(*, "names")= chr "RStudioGD"

2 个答案:

答案 0 :(得分:1)

paste或paste0会帮助你命名部分:

NAMES <- data.frame(paste0("Spring_VPCA_nbest=", 1:10),
               paste0("J:/R/Leaps/Spring_VPCA_nbest=", 1:10, ".png"))

我首先使用Python开始,所以我自然会使用比R更多的循环。所以虽然我确信有一个更好的方法使用lapply或类似的,这就是我要做的:

               nb_list <- list()
               for (i in 1:10) {
                  nb <- nbestfunc(SpringChalkLife, SPR_Q_PCA, G = i)
                  nb_list[[i]] <- nb
               }

               sum_list <- list()
               for (i in 1:length(nb_list)) {
                  ls <- leapsSummary(nb_list[[i]], B = NAMES[i,1], C = NAMES[i, 2])
                  sum_list[[i]] <- ls
                  dev.off()
               }

               lapply(sum_list, print)

在实践中,如果你可以替换一个length()或unique()调用,比如第二个循环,那么你可以更加防守,并将计数留给计算机。

答案 1 :(得分:0)

一个建议是将nb#s存储在命名列表中而不是作为单独的对象。像

这样的东西
nbList <- lapply(1:10, function(i) nbestfunc(SpringChalkLife, SPR_Q_PCA, G = i))
# give list items same names as before
names(nbList) <- paste0("nb", 1:10)

然后你可以很容易地将其余部分弹出到for循环中:

for(i in seq_len(10)) {
  leapsSummary(nbList[[i]], B = paste("Spring_VPCA_nbest=",i), 
               C = paste0("J:/R/Leaps/Spring_VPCA_nbest=, i, .png"))
  dev.off()
}

您也可以参考nbList名称,而不是像nbList[[paste0("nb",i)]]那样在循环中的位置。