[英]Nested user defined functions using apply in R
How would the following be written using apply? 以下内容将如何使用apply编写?
# Variables
age <- 1:100
Y <- age+5
d <- 0.25
dx <- 5
a_x <- 1:dx
Yd <- matrix( 0, nrow=max(age), ncol=dx )
# Nested loop is computationally inefficient?
for (a in age){
for (ax in a_x){
Yd[a,ax] <- (Y[[a]] * (1 - d) ** (ax-1))
}
}
My model has a lot of these nested for loop structures, because I am incompetent. 我的模型中有很多嵌套在循环结构中的,因为我不称职。 I am hoping to improve the computational time using apply.
我希望使用apply来缩短计算时间。 I find the apply functions rather confusing to get into.
我发现应用功能相当混乱。 I am looking for a solution that illustrates how one can obtain such nested structures using apply.
我正在寻找一种解决方案,以说明如何使用apply获得嵌套结构。 Hopefully, from there on I can apply (pun intended) the solution to even more complicated nested for loops (4-5 loops within each other).
希望从那里开始,我可以将解决方案应用于(甚至是双关语)更复杂的嵌套for循环(彼此之间有4-5个循环)。
For example 例如
Ydi <- rep( list(), 6)
for (i in 1:6){
Ydi[[i]] <- matrix( 0, nrow=max(age), ncol=dx )
}
# Nested loop is computationally inefficient?
for (i in 1:6){
for (a in age){
for (ax in a_x){
Ydi[[i]][a,ax] <- (Y[[a]] * (1 - d) ** (ax-1)) + i
}
}
}
I would use expand.grid
instead: 我将使用
expand.grid
代替:
df <- data.frame(expand.grid(a = age, ax = a_x))
df[['Yd']] <- (df[['a']] + 5) * (1 - d) ** (df[['ax']] - 1)
This is infinitely extendable (subject to memory constraints) - each additional nested loop will just be an additional variable in your expand.grid
call. 这是无限扩展的(受内存限制)-每个附加的嵌套循环将只是您
expand.grid
调用中的附加变量。 For example: 例如:
new_col <- 1:2
df_2 <- data.frame(expand.grid(a = age, ax = a_x, nc = new_col))
df_2[['Yd']] <- (df_2[['a']] + 5) * (1 - d) ** (df_2[['ax']] - 1) + df_2[['nc']]
This essentially switches to a tidy data format, which is an easier way of storing multi-dimensional data. 这实际上转换为整齐的数据格式,这是一种存储多维数据的简便方法。
For easier syntax, and faster speed, you can use the data.table
package: 为了
data.table
语法并提高速度,可以使用data.table
包:
library(data.table)
dt_3 <- data.table(expand.grid(a = age, ax = a_x, nc = new_col))
dt_3[ , Yd := (a + 5) * (1 - d) ** (ax - 1) + nc]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.