Post Reply 
moving data from Spreadsheet into 2-Var based on a category
01-21-2015, 06:19 AM
Post: #1
moving data from Spreadsheet into 2-Var based on a category
I have data in the Spreadsheet app (circumference of wrist in inches, height in inches, gender) and I'd like to do 2-Var stats using wrist and height values, but grouped by gender. I'd like to copy wrist values for males in C1, height values for males in C2, and wrist values for females in C3, and height values for females in C4. This way, I can do 2-Var stats on males and females separately.

But I'm stuck on how to do this copy/filter operation. Conceptually, I want something like "insert wrist values into C1 if gender is male", and so on. I'd like to avoid sorting the spreadsheet by gender, then copying cell regions from the Spreadsheet into 2-Var using the Home view. A program to do this would be cool (I'd learn a lot).

The wrist/height/gender columns contain:

Code:

6.5    69.5    "M"
6.5    72.25    "M"
6.5    67    "M"
6.25    62.5    "F"
7    73.25    "M"
6.75    66.75    "M"
5.75    61.5    "F"
6.75    63    "F"
6.5    65    "M"
7.25    65.75    "F"
6    68.5    "F"
7    68    "F"
6.5    66.25    "F"
6.75    71.5    "M"
6    63    "F"
5.8    64.25    "F"
6    66    "F"
6.25    67.25    "M"
6.5    67.75    "F"
6.75    63.5    "F"
7.5    71.5    "M"
7.25    67.5    "M"
Find all posts by this user
Quote this message in a reply
01-21-2015, 10:22 PM (This post was last modified: 01-21-2015 10:24 PM by Snorre.)
Post: #2
RE: moving data from Spreadsheet into 2-Var based on a category
Hello mbeddo,

Here's a simple program that is not very efficient/elegant but more a concept. It allows arbitrary count of rows and is easy to extend for new types (e.g. "C" for cats).
Code:
get_x_where_y_equals_z(x,y,z)
BEGIN
  LOCAL result:={},k;
  FOR k:=1 TO MIN(SIZE(x),SIZE(y)) DO
    IF y(k)=z THEN
      result:=CONCAT(result,x(k));
    END; 
  END;
  result;
END;

EXPORT DoStat()
BEGIN
  STARTAPP("Statistics 2Var");

  // Copy spreadsheet data to statistics app.
  C1:=get_x_where_y_equals_z(Spreadsheet.A:A,Spreadsheet.C:C,"M");
  C2:=get_x_where_y_equals_z(Spreadsheet.B:B,Spreadsheet.C:C,"M");
  C3:=get_x_where_y_equals_z(Spreadsheet.A:A,Spreadsheet.C:C,"F");
  C4:=get_x_where_y_equals_z(Spreadsheet.B:B,Spreadsheet.C:C,"F");

  // Define sets (S1=males, S2=females)
  SetIndep(S1,C1); SetDepend(S1,C2); CHECK(1);
  SetIndep(S2,C3); SetDepend(S2,C4); CHECK(2);

  // Show plot.
  STARTVIEW(9);
END;
Beware: your current app must be "Statistics 2Var" when entering/compiling. Otherwise you've to introduce a lot of "Statistics_2Var."-prefices into the code.
When running "DoStat", current app doesn't matter since it will always changed.

Greetings
Find all posts by this user
Quote this message in a reply
01-21-2015, 11:51 PM
Post: #3
RE: moving data from Spreadsheet into 2-Var based on a category
Hello Snorre,

Thank you! It works just fine.

I also have a TI nSpire, and the operation to filter a list based on a criteria is something that is lacking there as well, yet needing to filter lists comes up quite common in basic data analysis.

Functional languages typically have a function that is prototyped something like "filter(fun, x)" where "x" is a list of "things" and "fun" is a Boolean function that is called as we iterate through "x". The output is a filtered list - those elements in "x" that satisfy your criteria.

You kindly provided me with a "gem" (get_x_where_y_equals_z) which I will tuck away in a safe place. When I learn more about programming the HP Prime, maybe I'll end up with a more functional filter function.
Find all posts by this user
Quote this message in a reply
01-22-2015, 09:44 AM (This post was last modified: 01-22-2015 11:10 AM by Snorre.)
Post: #4
RE: moving data from Spreadsheet into 2-Var based on a category
Hello mbeddo,

the provided filter function was meant as a generic building block: easy to read, easy to understand, easy to customize.

A more efficient solution would probably not iterate four times over the row set but only once and distribute the values directly to C1, ..., C4. (Well, the Prime is so blazingly fast, one may not even notice a speedup).

If you're interested in the functional approach, take a closer look to the CAS function "select". (I think it doesn't improve speed or easy-to-understand'ness but is quite more fun to puzzle out a single expression doing the filter-task.)

Greetings
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)