简体   繁体   English

用 lapply() 替换嵌套的 for 循环

[英]replacing nested for loop with lapply()

I have sen this example to explain how to replace an nested for loop with the lapply() function.我用这个例子来解释如何用 lapply() 函数替换嵌套的 for 循环。 However i do not fully understand what is happening in the nested for loop?但是我不完全理解嵌套 for 循环中发生了什么?

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?根据我的理解,for 循环为所有年份的每个国家/地区创建了两个名为tempXtempY新变量,但是在 for 循环中参数的最后一行会发生什么?

what is the purpose of variable1 and variable2 ? variable1variable2的目的是什么?

# 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.在循环myData[myData$Country == ii & myData$Year == jj, c("computedFigure")] <- myFunction(tempX, tempY) ,函数myFunction被应用并记录在computedFigure列中.

variable1 and variable2 are set randomly to illustrate the data in myData (x and y) in myFunction . variable1 和 variable2 随机设置以说明myFunction中 myData(x 和 y)中的数据。

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. for 循环正在探索国家和年份的组合……这两个代码(for 循环和 lappy)不会产生完全相同的结果。 The lapply will generate a list just with the result of the myFunction . lapply 将生成一个列表,其中包含myFunction的结果。 The for loops will generate a dataframe. for 循环将生成一个数据帧。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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