Reputation: 91
I need to apply a custom function to multiple .txt files, the output of which looks as follows when applying to an individual .txt file:
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
When I use lapply
as follows:
filelist <- list.files(pattern = "*.txt")
datalist2 <- lapply(filelist, function(x)abs_fun)
I get a list of closures instead of data.frames (this is how my custom function looks):
[[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]]
...
How do I either get a list of data.frames (as in the example with "50609.txt"
) as a result instead or extract the outputs of the function from the closures?
Upvotes: 2
Views: 33
Reputation: 887108
The issue is with the function not being applied on the elements of the list
. Here, we have anonymous function (function(x) x
)
lapply(filelist, function(x)abs_fun(x))
or it can be directly applied without an anonymous call
lapply(filelist, abs_fun)
The OP's issue can be reproduced with
lapply(mtcars, function(x) mean)
instead it should be
lapply(mtcars, function(x) mean(x))
Upvotes: 3