lapply returns 闭包列表而不是 data.frames 列表
lapply returns list of closures instead of list of data.frames
我需要将自定义函数应用于多个 .txt 文件,当应用于单个 .txt 文件时,其输出如下所示:
abs_fun("50609.txt")
TIME SECCODE min(abs)
1 100000000 SU24018RMFS2 0.001374406
2 100000000 SU25081RMFS9 0.005432396
3 100000000 SU25082RMFS7 0.008767195
4 100000000 SU26203RMFS8 0.003786367
5 100000000 SU26205RMFS3 0.015636145
6 100000000 SU26206RMFS1 0.002658508
7 100000000 SU26207RMFS9 0.005674432
8 100000000 SU26208RMFS7 0.007532075
9 100000000 SU26212RMFS9 0.005923634
10 100000000 SU26215RMFS2 0.019073299
11 100000000 SU29006RMFS2 0.002031761
12 100000000 SU46020RMFS2 0.025543226
当我使用lapply
时如下:
filelist <- list.files(pattern = "*.txt")
datalist2 <- lapply(filelist, function(x)abs_fun)
我得到一个闭包列表而不是 data.frames(这是我的自定义函数的样子):
[[1]]
function (x)
{
data <- read.table(x, header = T, sep = ",")
buy <- subset(data, select = c("PRICE", "TIME", "ACTION",
"BUYSELL", "SECCODE", "VOLUME")) %>% filter(ACTION ==
1, BUYSELL == "B")
buy$ACTION = NULL
buy$BUYSELL = NULL
sell <- subset(data, select = c("PRICE", "TIME", "ACTION",
"BUYSELL", "SECCODE", "VOLUME")) %>% filter(ACTION ==
1, BUYSELL == "S")
sell$ACTION = NULL
sell$BUYSELL = NULL
buysell <- inner_join(x = buy, y = sell, by = c("SECCODE",
"TIME"), all = TRUE)
buysell$diff <- buysell$PRICE.y - buysell$PRICE.x
head(buysell, n = 100)
buysell <- group_by_at(buysell, vars(TIME, SECCODE))
summarise(buysell, min(diff))
buysell$abs <- (buysell$PRICE.y - buysell$PRICE.x)/(buysell$PRICE.y +
buysell$PRICE.x)/2
abs <- as.data.frame(summarise(buysell, min(abs)))
return(abs)
}
[[2]]
...
如何获取 data.frames 的列表(如 "50609.txt"
的示例)作为结果或从闭包中提取函数的输出?
问题在于函数未应用于 list
的元素。在这里,我们有匿名函数 (function(x) x
)
lapply(filelist, function(x)abs_fun(x))
也可以不匿名调用直接申请
lapply(filelist, abs_fun)
OP 的问题可以通过
重现
lapply(mtcars, function(x) mean)
应该是
lapply(mtcars, function(x) mean(x))
我需要将自定义函数应用于多个 .txt 文件,当应用于单个 .txt 文件时,其输出如下所示:
abs_fun("50609.txt")
TIME SECCODE min(abs)
1 100000000 SU24018RMFS2 0.001374406
2 100000000 SU25081RMFS9 0.005432396
3 100000000 SU25082RMFS7 0.008767195
4 100000000 SU26203RMFS8 0.003786367
5 100000000 SU26205RMFS3 0.015636145
6 100000000 SU26206RMFS1 0.002658508
7 100000000 SU26207RMFS9 0.005674432
8 100000000 SU26208RMFS7 0.007532075
9 100000000 SU26212RMFS9 0.005923634
10 100000000 SU26215RMFS2 0.019073299
11 100000000 SU29006RMFS2 0.002031761
12 100000000 SU46020RMFS2 0.025543226
当我使用lapply
时如下:
filelist <- list.files(pattern = "*.txt")
datalist2 <- lapply(filelist, function(x)abs_fun)
我得到一个闭包列表而不是 data.frames(这是我的自定义函数的样子):
[[1]]
function (x)
{
data <- read.table(x, header = T, sep = ",")
buy <- subset(data, select = c("PRICE", "TIME", "ACTION",
"BUYSELL", "SECCODE", "VOLUME")) %>% filter(ACTION ==
1, BUYSELL == "B")
buy$ACTION = NULL
buy$BUYSELL = NULL
sell <- subset(data, select = c("PRICE", "TIME", "ACTION",
"BUYSELL", "SECCODE", "VOLUME")) %>% filter(ACTION ==
1, BUYSELL == "S")
sell$ACTION = NULL
sell$BUYSELL = NULL
buysell <- inner_join(x = buy, y = sell, by = c("SECCODE",
"TIME"), all = TRUE)
buysell$diff <- buysell$PRICE.y - buysell$PRICE.x
head(buysell, n = 100)
buysell <- group_by_at(buysell, vars(TIME, SECCODE))
summarise(buysell, min(diff))
buysell$abs <- (buysell$PRICE.y - buysell$PRICE.x)/(buysell$PRICE.y +
buysell$PRICE.x)/2
abs <- as.data.frame(summarise(buysell, min(abs)))
return(abs)
}
[[2]]
...
如何获取 data.frames 的列表(如 "50609.txt"
的示例)作为结果或从闭包中提取函数的输出?
问题在于函数未应用于 list
的元素。在这里,我们有匿名函数 (function(x) x
)
lapply(filelist, function(x)abs_fun(x))
也可以不匿名调用直接申请
lapply(filelist, abs_fun)
OP 的问题可以通过
重现lapply(mtcars, function(x) mean)
应该是
lapply(mtcars, function(x) mean(x))