简体   繁体   中英

replacing nested for loop with lapply()

I have sen this example to explain how to replace an nested for loop with the lapply() function. However i do not fully understand what is happening in the nested for loop?

according to my understanding, the for loop creates for every country for all years two new variables called tempX and tempY , but what happens in the last line of the argument in the for loop?

what is the purpose of variable1 and variable2 ?

# Generate random data:
allCountries <- LETTERS[1:10]
allYears <- 1990:2012

myData <- expand.grid(allCountries, allYears)  # create a dataframe with all possible combinations
colnames(myData) <- c("Country", "Year")
myData$variable1 <- rnorm(nrow(myData))
myData$variable2 <- rnorm(nrow(myData))

# Silly function to perform
myFunction <- function(x, y){
  x * y - x / y
}

### Doubly-nested loop ###
myData$computedFigure <- NA  # Make an "empty" variable in my data.frame

for(ii in allCountries){
  for(jj in allYears){
    tempX <- myData[myData$Country == ii & myData$Year == jj, c("variable1")]
    tempY <- myData[myData$Country == ii & myData$Year == jj, c("variable2")]
    # "Save" results into appropriate location in my data.frame
    myData[myData$Country == ii & myData$Year == jj, c("computedFigure")] <- myFunction(tempX, tempY)
  }
}

### Simple lapply() approach ###
computedFigureList <- lapply(1:nrow(myData), function(x){
  tempX <- myData[x, c("variable1")]
  tempY <- myData[x, c("variable2")]
  # "Save" results into appropriate location in my data.frame
  myFunction(tempX, tempY)
})

myData$computedFigure2 <- unlist(computedFigureList)
with(myData, plot(computedFigure, computedFigure2))

In the last line of the loop myData[myData$Country == ii & myData$Year == jj, c("computedFigure")] <- myFunction(tempX, tempY) , the function myFunction is applied and recorded in the computedFigure column.

variable1 and variable2 are set randomly to illustrate the data in myData (x and y) in myFunction .

The for loops are exploring the combinations in countries and years... The two codes (for loop and lappy) will not generate exactly the same result. The lapply will generate a list just with the result of the myFunction . The for loops will generate a dataframe.

实际上你不需要嵌套的*apply函数,你实际上可以使用outer + diag来计算computedFigure ,它可以达到与嵌套for循环相同的结果。

myData$computedFigure <- diag(with(myData,outer(variable1,variable2,myFunction)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM