How to vectorize a for loop in R

2019-07-15 12:41发布

I'm trying to clean this code up and was wondering if anybody has any suggestions on how to run this in R without a loop. I have a dataset called data with 100 variables and 200,000 observations. What I want to do is essentially expand the dataset by multiplying each observation by a specific scalar and then combine the data together. In the end, I need a data set with 800,000 observations (I have four categories to create) and 101 variables. Here's a loop that I wrote that does this, but it is very inefficient and I'd like something quicker and more efficient.

datanew <- c()
for (i in 1:51){
  for (k in 1:6){
    for (m in 1:4){

      sub <- subset(data,data$var1==i & data$var2==k)

      sub[,4:(ncol(sub)-1)] <- filingstat0711[i,k,m]*sub[,4:(ncol(sub)-1)]

      sub$newvar <- m

      datanew <- rbind(datanew,sub)

    }
  }
}

Please let me know what you think and thanks for the help.

Below is some sample data with 2K observations instead of 200K

# SAMPLE DATA
#------------------------------------------------#
  mydf <- as.data.frame(matrix(rnorm(100 * 20e2), ncol=20e2, nrow=100))
  var1 <- c(sapply(seq(41), function(x) sample(1:51)))[1:20e2]
  var2 <- c(sapply(seq(2 + 20e2/6), function(x) sample(1:6)))[1:20e2]
  #----------------------------------#
  mydf <- cbind(var1, var2, round(mydf[3:100]*2.5, 2))
  filingstat0711 <- array(round(rnorm(51*6*4)*1.5 + abs(rnorm(2)*10)), dim=c(51,6,4))
#------------------------------------------------#

1条回答
对你真心纯属浪费
2楼-- · 2019-07-15 13:12

You can try the following. Notice that we replaced the first two for loops with a call to mapply and the third for loop with a call to lapply. Also, we are creating two vectors that we will combine for vectorized multiplication.

# create a table of the i-k index combinations using `expand.grid`
ixk <- expand.grid(i=1:51, k=1:6)

    # Take a look at what expand.grid does
    head(ixk, 60)


# create two vectors for multiplying against our dataframe subset
multpVec <- c(rep(c(0, 1), times=c(4, ncol(mydf)-4-1)), 0)
invVec   <- !multpVec

    # example of how we will use the vectors
    (multpVec * filingstat0711[1, 2, 1] + invVec)


# Instead of for loops, we can use mapply. 
newdf <- 
  mapply(function(i, k) 

    # The function that you are `mapply`ing is:
    # rbingd'ing a list of dataframes, which were subsetted by matching var1 & var2
    # and then multiplying by a value in filingstat
    do.call(rbind, 
        # iterating over m
        lapply(1:4, function(m)

          # the cbind is for adding the newvar=m, at the end of the subtable
          cbind(

            # we transpose twice: first the subset to multiply our vector. 
            # Then the result, to get back our orignal form
            t( t(subset(mydf, var1==i & mydf$var2==k)) * 
              (multpVec * filingstat0711[i,k,m] + invVec)), 

          # this is an argument to cbind
          "newvar"=m) 
    )), 

    # the two lists you are passing as arguments are the columns of the expanded grid
    ixk$i, ixk$k, SIMPLIFY=FALSE
  )

# flatten the data frame
newdf <- do.call(rbind, newdf)



Two points to note:

(1) Try not to use words like data, table, df, sub etc which are commonly used functions In the above code I used mydf in place of data.

(2) You can use apply(ixk, 1, fu..) instead of the mapply that I used, but I think mapply makes for cleaner code in this situation

Good luck, and welcome to SO

查看更多
登录 后发表回答