Col Sum
Col Sum using For
Example 1
Summary: Generates a custom data table by iterating through a list and inserting values based on specific conditions, utilizing For loop and Match function.
Code:
dt = Open("data_table.jmp");
xlist = {"1", "2", "3", "4"};
testVar = {};
For( i = 1, i <= N Items( xlist ), i++,
y = "sex";
If( i == 3, y = "height" );
Match( xlist[i],
xlist[1], Insert Into( testVar, Eval List( {i, y} ) ),
xlist[2], Insert Into( testVar, Eval List( {i, y} ) ),
xlist[3], Insert Into( testVar, Eval List( {i, y, Col Sum( Column( dt, y ) )} ) ),
xlist[4], Insert Into( testVar, Eval List( {i, y} ) ),
Insert Into( testVar, Eval List( {i, y, "error"} ) )
);
);
Code Explanation:
- Open data table.
- Define list
xlist. - Initialize empty list
testVar. - Loop through
xlistitems. - Set default
yto "sex". - Change
yto "height" for third iteration. - Match current
xlistitem. - Insert index and
yintotestVar. - For third item, insert sum of column
y. - Continue loop until completion.
Example 2
Summary: Generates a custom data table by matching specific values in an xlist and inserting corresponding information into a testVar list.
Code:
dt = Open("data_table.jmp");
xlist = {"1", "2", "3", "4"};
testVar = {};
For( i = 1, i <= N Items( xlist ), i++,
y = "sex";
If( i == 3, y = "height" );
MatchMZ( xlist[i],
xlist[1], Insert Into( testVar, Eval List( {i, y} ) ),
xlist[2], Insert Into( testVar, Eval List( {i, y} ) ),
xlist[3], Insert Into( testVar, Eval List( {i, y, Col Sum( Column( dt, y ) )} ) ),
xlist[4], Insert Into( testVar, Eval List( {i, y} ) ),
Insert Into( testVar, Eval List( {i, y, "error"} ) )
);
);
Code Explanation:
- Open data table;
- Define xlist with values.
- Initialize empty testVar list.
- Loop through xlist items.
- Set y to "sex".
- Change y to "height" for third item.
- Match xlist item to corresponding action.
- Insert i and y into testVar.
- For third item, add column sum of y.
- Handle unmatched cases by adding error message.
Example 3
Summary: Generates a custom data table with specific variables and calculations, utilizing a For loop and MatchV3 function.
Code:
dt = Open("data_table.jmp");
xlist = {"1", "2", "3", "4"};
testVar = {};
For( i = 1, i <= N Items( xlist ), i++,
y = "sex";
If( i == 3, y = "height" );
MatchV3( xlist[i],
xlist[1], Insert Into( testVar, Eval List( {i, y} ) ),
xlist[2], Insert Into( testVar, Eval List( {i, y} ) ),
xlist[3], Insert Into( testVar, Eval List( {i, y, Col Sum( Column( dt, y ) )} ) ),
xlist[4], Insert Into( testVar, Eval List( {i, y} ) ),
Insert Into( testVar, Eval List( {i, y, "error"} ) )
);
);
Code Explanation:
- Open data table.
- Define xlist with four items.
- Initialize empty list testVar.
- Start loop from 1 to 4.
- Set y to "sex".
- If i equals 3, set y to "height".
- Match xlist item with cases.
- For xlist[1] or [2], add {i, y} to testVar.
- For xlist[3], add {i, y, column sum} to testVar.
- For xlist[4], add {i, y} to testVar.