在R中一次运行多个条件

Pie*_*rre -3 r

我编写了一个代码来将函数应用于数据框input:

    set.seed(1234) 
    n = 5000000
    input <- as.matrix(data.frame(c1 = sample(1:10, n, replace = T), c2 = sample(1:10, n, replace = T), c3 = sample(1:10, n, replace = T), c4 = sample(1:10, n, replace = T)))

    system.time(
    test <- input %>% 
      split(1:nrow(input)) %>% 
      map(~ func1(.x, 2, 2, "test_1")) %>% 
      do.call("rbind", .))

## Here is the function used:

    func1 <- function(dataC, PR, DB, MT){

          c1 <- as.vector(dataC[1])
          c2 <- as.vector(dataC[2])

          c3 <- as.vector(dataC[3])
          c4 <- as.vector(dataC[4])

          newc1 <- -999
          newc2 <- -999

          if(MT=="test_1"){

            listC <- expand.grid(x = c((c1 - PR) : (c1 - 1)), y = c((c2 + 1) : (c2 + PR)))
            V1 <- mean(sample(1:10, size = dim(listC)[1], replace = TRUE)) * sqrt(2) * DB

            listC <- expand.grid(x = c((c1 - 1) : (c1 + 1)), y = c((c2 + 1) : (c2 + PR)))
            V2 <- mean(sample(1:10, size = dim(listC)[1], replace = TRUE)) * DB

            listC <- expand.grid(x = c((c1 + 1) : (c1 + PR)), y = c((c2 + 1) : (c2 + PR)))
            V3 <- mean(sample(1:10, size = dim(listC)[1], replace = TRUE)) * sqrt(2) * DB

            listC <- expand.grid(x = c((c1 - PR) : (c1 - 1)), y = c((c2 - 1) : (c2 + 1)))
            V4 <- mean(sample(1:10, size = dim(listC)[1], replace = TRUE)) * DB

            V5 <- 0

            listC <- expand.grid(x = c((c1 + 1) : (c1 + PR)), y = c((c2 - 1) : (c2 + 1)))
            V6 <- mean(sample(1:10, size = dim(listC)[1], replace = TRUE)) * DB

            listC <- expand.grid(x = c((c1 - PR) : (c1 - 1)), y = c((c2 - PR) : (c2 - 1)))
            V7 <- mean(sample(1:10, size = dim(listC)[1], replace = TRUE)) * sqrt(2) * DB

            listC <- expand.grid(x = c((c1 - 1) : (c1 + 1)), y = c((c2 - PR) : (c2 - 1)))
            V8 <- mean(sample(1:10, size = dim(listC)[1], replace = TRUE)) * DB

            listC <- expand.grid(x = c((c1 + 1) : (c1 + PR)), y = c((c2 - PR) : (c2 - 1)))
            V9 <- mean(sample(1:10, size = dim(listC)[1], replace = TRUE)) * sqrt(2) * DB


          } else if(MT=="test_2"){

            listC <- expand.grid(x = c((c1 - PR) : (c1 - 1)), y = c((c2 + 1) : (c2 + PR)))
            V1 <- harmonic.mean(sample(1:10, size = dim(listC)[1], replace = TRUE)) * sqrt(2) * DB

            listC <- expand.grid(x = c((c1 - 1) : (c1 + 1)), y = c((c2 + 1) : (c2 + PR)))
            V2 <- harmonic.mean(sample(1:10, size = dim(listC)[1], replace = TRUE)) * DB

            listC <- expand.grid(x = c((c1 + 1) : (c1 + PR)), y = c((c2 + 1) : (c2 + PR)))
            V3 <- harmonic.mean(sample(1:10, size = dim(listC)[1], replace = TRUE)) * sqrt(2) * DB

            listC <- expand.grid(x = c((c1 - PR) : (c1 - 1)), y = c((c2 - 1) : (c2 + 1)))
            V4 <- harmonic.mean(sample(1:10, size = dim(listC)[1], replace = TRUE)) * DB

            V5 <- 0

            listC <- expand.grid(x = c((c1 + 1) : (c1 + PR)), y = c((c2 - 1) : (c2 + 1)))
            V6 <- harmonic.mean(sample(1:10, size = dim(listC)[1], replace = TRUE)) * DB

            listC <- expand.grid(x = c((c1 - PR) : (c1 - 1)), y = c((c2 - PR) : (c2 - 1)))
            V7 <- harmonic.mean(sample(1:10, size = dim(listC)[1], replace = TRUE)) * sqrt(2) * DB

            listC <- expand.grid(x = c((c1 - 1) : (c1 + 1)), y = c((c2 - PR) : (c2 - 1)))
            V8 <- harmonic.mean(sample(1:10, size = dim(listC)[1], replace = TRUE)) * DB

            listC <- expand.grid(x = c((c1 + 1) : (c1 + PR)), y = c((c2 - PR) : (c2 - 1)))
            V9 <- harmonic.mean(sample(1:10, size = dim(listC)[1], replace = TRUE)) * sqrt(2) * DB

          }

          tot <- sum(c(1/V1, 1/V2, 1/V3, 1/V4, 1/V6, 1/V7, 1/V8, 1/V9), na.rm = TRUE)
          mat_V <- matrix(data = c((1/V1)/tot, (1/V2)/tot, (1/V3)/tot, (1/V4)/tot, V5, 
                                        (1/V6)/tot, (1/V7)/tot, (1/V8)/tot, (1/V9)/tot), nrow = 3, ncol = 3, byrow = TRUE)

          while((newc1 == -999 && newc2 == -999) || (c3 == newc1 && c4 == newc2)){

            if(c3 == newc1 && c4 == newc2){
              mat_V[choiceC[1], choiceC[2]] <- NaN
              ## print(mat_V)
            }

            choiceC <- which(mat_V == max(mat_V, na.rm = TRUE), arr.ind = TRUE)
            ## print(choiceC)
            ## If there are several maximum values
            if(nrow(choiceC) > 1){
              choiceC <- choiceC[sample(1:nrow(choiceC), 1), ]
            }

            if(choiceC[1]==1 & choiceC[2]==1){

              newC <- matrix(c(x = c1 - 1, y = c2 + 1), ncol = 2)

            } else if(choiceC[1]==1 & choiceC[2]==2){

              newC <- matrix(c(x = c1, y = c2 + 1), ncol = 2)

            } else if(choiceC[1]==1 & choiceC[2]==3){

              newC <- matrix(c(x = c1 + 1, y = c2 + 1), ncol = 2)

            } else if(choiceC[1]==2 & choiceC[2]==1){

              newC <- matrix(c(x = c1 - 1, y = c2), ncol = 2)

            } else if(choiceC[1]==2 & choiceC[2]==3){

              newC <- matrix(c(x = c1 + 1, y = c2), ncol = 2)

            } else if(choiceC[1]==3 & choiceC[2]==1){

              newC <- matrix(c(x = c1 - 1, y = c2 - 1), ncol = 2)

            } else if(choiceC[1]==3 & choiceC[2]==2){

              newC <- matrix(c(x = c1, y = c2 - 1), ncol = 2)

            } else if(choiceC[1]==3 & choiceC[2]==3){ 

              newC <- matrix(c(x = c1 + 1, y = c2 - 1), ncol = 2)
            }

            newc1 <- as.vector(newC[,1])
            newc2 <- as.vector(newC[,2])

          }

          return(newC)

        }
Run Code Online (Sandbox Code Playgroud)

该代码适用于小型数据集,但当数据框包含超过100万行时,它非常慢.我认为在函数(例如条件if else)中重复有许多代码行会降低速度.有没有办法立即在函数中进行所有计算?我真的很感激任何建议.

Moo*_*per 10

首先是一些强烈的爱,但我强烈建议你覆盖你的基础,你的代码是不良做法的集中,通过花费一些时间研究矢量化等你将获得巨大的投资回报率...考虑也发布在https上://codereview.stackexchange.com/questions/tagged/r下次因为这是一个更合适的问题.

你的瓶颈不是嵌套的ifs,而是使用不足expand.grid.

您在代码中创建数据框,通过expand.grid您不正确地调用listC(它们不是列表).然后,这个代价高昂的data.frame只用于它的行数,你dim(listC)[1]会得到更多的惯用类型nrow(listC).

此值(dim(listC)[1])只能PR^2或3*PR在实践中,因此您可以先计算它们,然后重复使用它们.

嵌套的ifs可以用嵌套的switch语句替换,更具可读性,并且只有在我们提高效率时才测试第一个选择.

它允许我们看到您在代码中忘记了一个条件.请参阅下面的改进代码.

当它看起来更整洁时,我们看到我们实际上可以简单地替换它newC <- c(c1 - 2 + choice[2], c2 + 2 - choice[1]).

补充意见

  • 评论您的代码,而不是我们,为您(以及当您决定发布问题时为我们)
  • c2 <- as.vector(dataC[2]) 可以替换为 c2 <- dataC[[2]]
  • 2列,一列的矩阵可以通过内置t(c(1,2))代替matrix(c(x = 1, y = 2), ncol = 2),但如果你打算使用as.vector到底就可以了,做c(1,2)摆在首位
  • 代码可能会进一步优化

修改后的代码

func1 <- function(dataC, PR, DB, MT){

  c1 <- dataC[[1]]
  c2 <- dataC[[2]]
  c3 <- dataC[[3]]
  c4 <- dataC[[4]]

  fun  <- if(MT=="test_1") mean else if(MT=="test_2") harmonic.mean
  fun2 <- function(size,mult)
    fun(sample(1:10, size = size, replace = TRUE)) * mult

  pr_sq <- PR^2
  pr_3 <- 3*PR
  sqrt_2_DB <- sqrt(2) * DB
  V1 <- fun2(pr_sq, sqrt_2_DB)
  V2 <- fun2(pr_3, DB)
  V3 <- fun2(pr_sq, sqrt_2_DB)
  V4 <- fun2(pr_3, DB)
  V5 <- 0
  V6 <- fun2(pr_3,  DB)
  V7 <- fun2(pr_sq, sqrt_2_DB)
  V8 <- fun2(pr_3,  DB)
  V9 <- fun2(pr_sq, sqrt_2_DB)

  inv <- 1/c(V1, V2, V3, V4, V6, V7, V8, V9)
  tot <- sum(inv, na.rm = TRUE)
  mat_V <- matrix(data = c(inv[1:4], V5, inv[5:8]) / tot, 
                  nrow = 3, ncol = 3, byrow = TRUE)

  newC <- NULL
  while(is.null(newC) || identical(c(c3,c4), newC)){

    if(identical(c(c3,c4), newC)){
      mat_V[choiceC[1], choiceC[2]] <- NaN
      ## print(mat_V)
    }

    choiceC <- which(mat_V == max(mat_V, na.rm = TRUE), arr.ind = TRUE)
    ## print(choiceC)
    ## If there are several maximum values
    if(nrow(choiceC) > 1){
      choiceC <- choiceC[sample(1:nrow(choiceC), 1), ]
    }

    newC <- c(c1 - 2 + choiceC[2], c2 + 2 - choiceC[1])

    # using switch it would have been
    # newC <- switch(choiceC[1],
    #        `1` = switch(choiceC[2],
    #                     `1` = c(x = c1 - 1, y = c2 + 1),
    #                     `2` = c(x = c1, y = c2 + 1),
    #                     `3` = c(x = c1 + 1, y = c2 + 1)),
    #        `2` = switch(choiceC[2],
    #                     `1` = c(x = c1 - 1, y = c2),
    #                     `2` = c(x = c1, y = c2), # you were missing this one
    #                     `3` = c(x = c1 + 1, y = c2)),
    #        `3` = switch(choiceC[2],
    #                     `1` = c(x = c1 - 1, y = c2 - 1),
    #                     `2` = c(x = c1, y = c2 - 1),
    #                     `3` = c(x = c1 + 1, y = c2 - 1)))
  }
  t(newC)
}
Run Code Online (Sandbox Code Playgroud)