Reputation: 45
I am trying to calculate the average of elements of lists. Therefor I have to add all the first elements of ten lists separate and the second element.. etc.
So far I have this:
set averagec map [ [x] -> item 0 x] finaltable
set averaged map [ [x] -> item 1 x] finaltable
set averagegemiddelderan map [ [x] -> item 2 x] finaltable
set averageunf map [ [x] -> item 3 x] finaltable
set averagetft map [ [x] -> item 4 x] finaltable
set averagepav map [ [x] -> item 5 x] finaltable
set averagetftt map [ [x] -> item 6 x] finaltable
set averagertft map [ [x] -> item 7 x] finaltable
set averageswit map [ [x] -> item 8 x] finaltable
set averagehm map [ [x] -> item 9 x] finaltable
Suggestions?
Upvotes: 0
Views: 76
Reputation: 9620
Here are two ways to produce a list that holds the means across the rows, which I believe is what you asked for. The first method adds up the rows and then divides by the number of rows. The second method transposes the list of lists (which is apparently the structure of your finaltable
) and then maps mean
across the result. The finaltable
must be rectangular in both cases.
globals [finaltable]
to setup
;fake data (rectangular list of list)
set finaltable n-values 10 [-> n-values 15 [-> random 100]]
end
to go
;first method
let _sum reduce [[?xs ?ys] -> addvecs ?xs ?ys] finaltable
let _n length finaltable
print map [?x -> ?x / _n] _sum
;second method
print map mean transpose finaltable
end
;two helper functions, for the two methods
to-report addvecs [#xs #ys]
report (map + #xs #ys)
end
to-report transpose [#lstlst]
let _n length item 0 #lstlst
let _start fput n-values _n [-> []] #lstlst
report reduce [[?lstlst ?items] -> (map [[?lst ?item] -> lput ?item ?lst] ?lstlst ?items)] _start
end
Upvotes: 1