Array average
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 33
- Joined: Tue Apr 25, 2017 8:13 pm
Array average
Hi there!
I'm trying to get the average of an array...
My array is made from the contents of 3 fields and my problem is that I want the average to consider the number of fields with some imput made.
--for example if I have 100, 99, 101 my average is 100 but if I only have 100, 99 my average will be 66.
Must be simple stupid to solve, sorry for being dumb.
I'm trying to get the average of an array...
My array is made from the contents of 3 fields and my problem is that I want the average to consider the number of fields with some imput made.
--for example if I have 100, 99, 101 my average is 100 but if I only have 100, 99 my average will be 66.
Must be simple stupid to solve, sorry for being dumb.
Re: Array average
What did you script so far?
-
- Posts: 33
- Joined: Tue Apr 25, 2017 8:13 pm
Re: Array average
Code: Select all
repeat with x = 1 to x
put field "PTx" into tPTarray [x]
end repeat
put average (tPTarray) into field "MEAN1"
EDIT: just noticed that my script didn't work now. I made this "repeat" script to simplify my
Code: Select all
put field "PT1" into tPTarray [1]
I tested and it worked because it had values already, when I changed it I got 0 in the average...
Re: Array average
Did you really use:
...
repeat with x = 1 to x
...
?
...
repeat with x = 1 to x
...
?
Re: Array average
Ah, OK, confirmed.
Not sure if this is a bug or desired behavior?
Not sure if this is a bug or desired behavior?
Re: Array average
It is desired behaviour!
From the dicntionary about "average":
...
The average function can also be written like this:
sum(numbersList)/the number of items in numbersList
...
Means EMPTY items (array keys) are also counted.
So you need to check for empty fields before filling the array!
From the dicntionary about "average":
...
The average function can also be written like this:
sum(numbersList)/the number of items in numbersList
...
Means EMPTY items (array keys) are also counted.
So you need to check for empty fields before filling the array!
-
- Posts: 33
- Joined: Tue Apr 25, 2017 8:13 pm
Re: Array average
with an if, then function?
Re: Array average
If / then or case, which ever is your preference.
-
- VIP Livecode Opensource Backer
- Posts: 9658
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Array average
I thought the issue was that the OP wanted to get the average of an array. The average function will only work with clear data.
Unless I am just not getting it, the array must be converted to an ordinary dataSet with the "combine" command, and then the average function can be used. And yes, an empty field must be considered both to exist and to contain "0".
Craig Newman
Unless I am just not getting it, the array must be converted to an ordinary dataSet with the "combine" command, and then the average function can be used. And yes, an empty field must be considered both to exist and to contain "0".
Craig Newman
Last edited by dunbarx on Sun Sep 09, 2018 2:54 pm, edited 1 time in total.
Re: Array average
You could be right, I don't work much with arrays myself.
Re: Array average
Take a look into the dictionary, arrays are also supported with "average()".
-
- VIP Livecode Opensource Backer
- Posts: 9658
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Array average
Klaus.
Ah.
Craig
Ah.
Craig
-
- Posts: 33
- Joined: Tue Apr 25, 2017 8:13 pm
Re: Array average
Just a follow up...
I solved my issue like this:
With this I can do means and stdev of the array correctly
Thank you for your help guys.
I solved my issue like this:
Code: Select all
repeat with x=1 to the number of fields in group "grp0"
if field x of group "grp0" is not "" then
put field x of group "grp0" into tArrayPT[x]
end if
end repeat
Thank you for your help guys.