我需要将自定义函数应用于多个.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.frame列表(如带有"50609.txt"
的示例)或从闭包中提取函数的输出?
答案 0 :(得分:3)
问题在于该函数未应用于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))