Turn on suggestions

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

Showing results for

- JMP User Community
- :
- Discussions
- :
- Re: JSL script - How to create a column with repeating values?

Topic Options

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

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

Apr 1, 2019 8:45 AM
(2465 views)

Hi,

despite looking into many discussions / the scripting index .., I could not get an answer, how to write a JSL script, that will take over the new column command enabling to enter sequence data with N times repetition of each value. I know how to do it manually - thanks to a reply from Mark B... in one of those discussions, but I struggle to write a script

e.g.

N_DOE= 10

N_XRF =4

=> {1,1,1,1,2,2,2,2,3,3,3,3, ...10,10,10,10}

```
//How may I get a column with repeating values
dt=current data table();
N_DOE=10;
N_XRF=4;
seq = sequence(1,N_DOE,1,N_XRF);
dt << new column("Run", "numeric", "ordinal");
//dt:run << set each value(seq,); // this works, but it is just"1" for all rows
dt:run << set each value(i=1,i<N_DOE,i++, seq);
//dt:run << set each value(for(i=1,i<N_DOE,i++, seq));
//dt << new column("Run", "numeric", "ordinal", values(repeat(seq));
//dt << new column("Run", "numeric", "ordinal", values(repeat(for(i=1,i<N_DOE,i++, seq))));
//dt << new column("Run", "numeric", "ordinal", for each row(for(i=1,i<N_DOE,i++, seq)));
//dt << new column("Run", "numeric", "ordinal", for each row(seq));
```

1 ACCEPTED SOLUTION

Accepted Solutions

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

The Sequence() function really only works within a formula. Take a look at it in the Scripting Index

Help==>Scripting Index.......Sequence

Here is a working example

```
names default to here(1);
dt=open("$SAMPLE_DATA/big class.jmp");
N_DOE=10;
N_XRF=4;
dt << new column("Run", "numeric", "ordinal");
// Create the JSL required to set the format and then
// execute it
Eval( Parse(
"dt:run << set formula( sequence(1, " || char(N_DOE) ||
", ince = 1, n = " || char(N_XRF) || "));"
)
);
```

Jim

2 REPLIES 2

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

The Sequence() function really only works within a formula. Take a look at it in the Scripting Index

Help==>Scripting Index.......Sequence

Here is a working example

```
names default to here(1);
dt=open("$SAMPLE_DATA/big class.jmp");
N_DOE=10;
N_XRF=4;
dt << new column("Run", "numeric", "ordinal");
// Create the JSL required to set the format and then
// execute it
Eval( Parse(
"dt:run << set formula( sequence(1, " || char(N_DOE) ||
", ince = 1, n = " || char(N_XRF) || "));"
)
);
```

Jim

Highlighted
##

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

Re: JSL script - How to create a column with repeating values?

@PS_Ato, Jim, @txnelson has already provided a solution.

The JMP functions,** Sequence()** and** Count() **are very cool functions when working with a data table, since both functions rely on the **Row()** of the current data table.

There might be times when you want the sequence in just a matrix. One method is the matrix function **Direct Product(amat, bmat)** . The script below is similar to your original script; it creates **seq** a vector of repeated values, then assigns them to the new column.

This is provided as an FYI. If you have not used matrices or studied linear algebra, this would not be the first function that might pop into your mind.

```
Names Default to Here(1);
N_DOE=10;
N_XRF=4;
New Table("Dummy", add rows(N_DOE * N_XRF));
dt=current data table();
seq = Transpose(Direct Product(Index(1,N_DOE),J(1,N_XRF,1)));
dt << new column("Run", "numeric", "ordinal", values(seq));
/* Log output
seq = Transpose(Direct Product(Index(1,N_DOE),J(1,N_XRF,1)));
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10]
*/
```