BookmarkSubscribeRSS Feed
Choose Language Hide Translation Bar
uday_guntupalli
Community Trekker

Re: How to Separate Matching Values in a List

@Jaz,
      This is an alternative approach, with the use of Loc().

MyList = {22,33,22,22,22,33,33,44,44,44,33,22}; 
AA = Associative Array(MyList); 
UniqueVals = AA << Get Keys; 
Frequency = {}; 

for(i = 1, i <= N Items(UniqueVals),i++,
		Insert Into(Frequency,N Rows(Loc(MyList,UniqueVals[i])));
   );
Show(Frequency);

 

Best
Uday
Jaz
Jaz
Community Trekker

Re: How to Separate Matching Values in a List

Hi @pmroz,

I tried the first solution you posted and it works fine with one file / list. But when I use your code as a function and loop through it, (despite having initialised all my variables to make sure that nothing from the previous iteration stored) it doesn't seem to be working. I'm referencing to a file because I loop through a column in the file to get the list being used. Any ideas on what's going on as I've made sure to initialise all the lists and variables?

0 Kudos
pmroz
Super User

Re: How to Separate Matching Values in a List

Can you post some data and code?

0 Kudos
Jaz
Jaz
Community Trekker

Re: How to Separate Matching Values in a List

Hi,

 

No worries, I solved the ssue and your solution wasn't part of the problem. Thanks.

0 Kudos
pmroz
Super User

Re: How to Separate Matching Values in a List

If you're looping through a column in a file why not just use the Tabulate or Summary commands?   If you are grouping things by another column you can use that as a "by" variable.  For example:

ID Value
A 1
A 2
A 2
B 2
B 3
B 3
B 4
B 4

 

Using tabulate drag the ID and Value columns over the drop zone for rows.  You'll need to change the role of Value to be a grouping variable.  This is the result:

ID Value N
A 1 1
A 2 2
B 2 1
B 3 2
B 4 2

So for A you have two distinct values (1, 2), along with the number of occurrences (1, 2).  For B you have three distinct values (2, 3, 4), along with occurrences (1, 2, 2).

Here's the equivalent JSL:

dt = New Table( "Test", Add Rows( 8 ), 
		New Column( "ID", Character, "Nominal",
		Set Values( {"A", "A", "A", "B", "B", "B", "B", "B"} ) ),
	New Column( "Value", Numeric, "Continuous", Format( "Best", 12 ),
		Set Values( [1, 2, 2, 2, 3, 3, 4, 4] ) )
);

dtab = dt << Tabulate(
	Show Control Panel( 0 ),
	Add Table( Row Table( Grouping Columns( :ID, :Value ) ) ),
	invisible
	);

dt2 = dtab << Make Into Data Table;
0 Kudos
vince_faller
Super User

Re: How to Separate Matching Values in a List

I'm a big fan of design.  

names default to here(1);
l = {22, 22, 22, 33, 33};
l = sort ascending(l);
d = VSuM(design(l));
csum = cumulative sum(d);
lvalues = l[csum];

aa = associative array(lvalues, d);

*Edit* I realized that sorting was taking a lot of time so this is a lot faster

names default to here(1);
my_list = {44, 33, 22, 33, 44};
aa = associative array(my_list );
keys = aa << Get Keys;
d = design(my_list , keys);
aa_design = associative array(keys, VSum(d));

 

*Edit again (don't know why this is bothering me so much)* I didn't like the associative array and this is faster still

 

names default to here(1);
my_list = {44, 33, 22, 33, 44};
d = design(my_bigger_list, <<Levels);
aa_design = associative array(d[2], VSum(d[1]));

Vince Faller - Predictum
txnelson
Super User

Re: How to Separate Matching Values in a List

I believe your last edit needs to have the "my_bigger_list" changed to "my_list"

Jim
0 Kudos