Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

- JMP User Community
- :
- Discussions
- :
- Sum Amount of Consecutive Zeros in a Column

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Sum Amount of Consecutive Zeros in a Column

Jan 10, 2020 6:34 AM
(571 views)

Hello JMP Community,

I am currently working on a project that randomly outputs either a "0" or "1" for 100 different time values. Each run of this corresponds to a "seed". I need to somehow code something that counts the number of consecutive 0's starting from a time point for each seed, and then prints it in a data table. If the seed being evaluated at a certain time starts with a 1, its sum is 0. From there, once all of these sums of consecutive 0's are found, I need to find its average (the average of zeroes before a 1 occurs). Attached below is the code I have so far (it only creates the seeds and time values).

Thanks,

Kyle

Close All( Data Tables, No Save );

//Make the data table

dt = New Table( "Case 1",

Add Rows( 100 ),

New Column( "Time", Set Formula( Sequence( 1, 100 ) ) ),

New Column( "Seed 1", Numeric, "Continuous", Format( "Best", 10 ), Formula( Random Integer( 0, 1 ) ) )

);

For( i = 2, i < 101, i++,

dt << New Column( "Seed " || Char( i ), Numeric, Continuous, Formula( Random Integer( 0, 1 ) ) ),

);

3 REPLIES 3

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Sum Amount of Consecutive Zeros in a Column

How do you want the average counts as output? Here's a way that puts it into an associative array:

```
seed_avg = Associative Array();
nseed = 100;
for(i = 1, i <= nseed, i++,
col_name = "Seed " || char(i);
col = Column(dt, col_name );
count_vec = [];
count = 0;
for(j = 1, j <= N Row(dt), j++,
if(col[j] == 0,
count++,
if(col[j] == 1 & count != 0,
count_vec = count_vec |/ count;
count = 0
);
)
);
Insert Into(seed_avg, col_name, mean(count_vec));
);
```

This can be easily adapted to output to a table or whatever format you prefer.

-- Cameron Willden

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Sum Amount of Consecutive Zeros in a Column

Thank you for the detailed response -- however, I am still having some trouble. From my understanding, the Associative Array makes a key... For example, the output contains "["Seed 1"] => 2.143"

Since each run corresponds to a time value, shouldn't it look something like "["Time 1" => 2.143"]?

Also, how exactly would I get it adapted to a table?

I tried dt << New Column("Mean", Set Formula(seed_avg)); , but that gives me a whole list of values for each entry in the column.

Thanks

Since each run corresponds to a time value, shouldn't it look something like "["Time 1" => 2.143"]?

Also, how exactly would I get it adapted to a table?

I tried dt << New Column("Mean", Set Formula(seed_avg)); , but that gives me a whole list of values for each entry in the column.

Thanks

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Sum Amount of Consecutive Zeros in a Column

Sorry, I didn't understand that you were wanting to go across rows rather than down each column. Here's a way to get it by time point with the results in a new column.

```
dt << New Column("Average", numeric);
nseed = 100;
for(i = 1, i <= N Row(dt), i++,
count_vec = [];
count = 0;
for(j = 1, j <= nseed, j++,
col = Column(dt, "Seed " || char(j) );
if(col[i] == 0,
count++,
if(col[j] == 1 & count != 0,
count_vec = count_vec |/ count;
count = 0
);
)
);
dt:Average[i] = mean(count_vec);
);
```

-- Cameron Willden

Article Labels

There are no labels assigned to this post.