Retrieving Results or Results

I am currently using numerous lines that look like this:

test=data.frame(t=seq(1,5,1),e=seq(6,10,1))
mean(apply(test,2,mean))

I want to convert the second line to mclapply, which gives the same result as lapply. I understand that I can extract each element from the lapply statement using a for loop and then use the average value for this vector, but this will slow down the performance I'm trying to improve with mclapply. The problem is that both lapply and mcapply return a list, which means they cannot be used. I can either use [[]] to get the actual value, or check $ t and test $ e, but the number of columns in the test variable is usually more than 1000. There should be an easier way to handle this. Basically, I want to get the average of this statement:

mclapply(test,mean,mc.preschedule=TRUE)

preferably without creating new variables or using for loops. The solution should be equivalent to getting the average of this statement:

lapply(test,mean)
+3
source share
2 answers

I'm confused - but data.frame- it's all the same list. So, apart from the obvious

R> testdf <- data.frame(t=seq(1,5,1),e=seq(6,10,1))
R> mean(testdf)
t e 
3 8 
R> mean(mean(testdf))
[1] 5.5
R> 

you can also do

R> lapply(testdf, mean)
$t
[1] 3

$e
[1] 8

R> mean(unlist(lapply(testdf, mean)))
[1] 5.5
R> 

So, for the inside lapplyyou can use mclapplyas you wish, no?

+2
source

I like to show the results mclapply()in a list, and then combine these lists to form the final product:

results.list <- list()
results.list <- mclapply(listOfData, analysisFunction, mc.cores = 7)

library(data.table)
result <- rbindlist(results.list) 
0
source

Source: https://habr.com/ru/post/1762539/


All Articles