为了进一步补充比斯特菲尔德的答案,您似乎想对每个数据帧进行一些复杂的操作。
在 apply 语句中可以有复杂的函数。所以你现在有:
for (i in dflist) {
# Do some complex things
}
这可以翻译为:
lapply(dflist, function(df) {
# Do some complex operations on each data frame, df
# More steps
# Make sure the last thing is NULL. The last statement within the function will be
# returned to lapply, which will try to combine these as a list across all data frames.
# You don't actually care about this, you just want to run the function.
NULL
})
使用情节的更具体的例子:
# Assuming we have a data frame with our points on the x, and y axes,
lapply(dflist, function(df) {
x2 <- df$x^2
log_y <- log(df$y)
plot(x,y)
NULL
})
您还可以编写带有多个参数的复杂函数:
lapply(dflist, function(df, arg1, arg2) {
# Do something on each data.frame, df
# arg1 == 1, arg2 == 2 (see next line)
}, 1, 2) # extra arguments are passed in here
希望这可以帮助你!