您好,我遇到以下挑战,无法编写脚本。
我的起始数据表如下所示。 我想从 col2 =0 开始向后乘 Xi 作为标准,并将新值放入新列中。
列 1 列 2
X1 0
X2 y2
X3 y3
……
Xn yn
XX1 0
XX2年2月
……
XXn yyn
……
……
我的最终数据表如下所示:
列1 列2 列3
X1 0 Xn*Xn-1*……X3*X2*X1
X2 y2 Xn*Xn-1*…..X3*X2
X3 y3 Xn*Xn-1*….*X3
……
Xn yn Xn
XX1 0 XXn*XXn-1*…..XX2*XX1
XX2 yy2 XXn*XXn-1*…..XX2
……
XXn yyn XXn
……
……
有人可以帮忙吗? 非常感谢!
如果数据颠倒,您想要的计算就很容易完成,因此这里有一个小脚本,可以使用该想法计算值
names default to here(1);
// create the sample table
dt = New Table( "example",
Add Rows( 10 ),
New Column( "col1",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values( [1, 2, 3, 2, 4, 5, 6, 7, 8, 9] ),
Set Display Width( 48 )
),
New Column( "col2",
Character( 1 ),
"Nominal",
Set Values( {"x", "x", "x", "y", "y", "y", "y", "z", "z", "z"} ),
Set Display Width( 45 )
)
);
// JSL to create the column you want
// create a new column to preserve the order of the data
dt << new column("origRow", formula(row()));
dt:origRow << delete formula;
// Reverse the order of the data
dt << sort(by(:origRow), order(descending), replace table(1));
// create the new column
dt << new column( "col3", formula(
If( Row() == 1, x = :col1 );
If( Lag( :col2 ) != :col2,
x = :col1,
x = x * :col1
);
x;
));
// Since the formula is dependent on the order of the data, the formula needs
// to be removed and the values set to static values
dt:col3 << delete formula;
// reorder the data table back to the original order and delete the origRow column
dt << sort(by(:origRow), order(ascending), replace table(1));
dt << delete columns(:origRow);
这是一段 JSL,它将创建新列而不进行排序
Names Default To Here( 1 );
// create the sample table
dt = New Table( "example",
Add Rows( 10 ),
New Column( "col1",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values( [1, 2, 3, 2, 4, 5, 6, 7, 8, 9] ),
Set Display Width( 48 )
),
New Column( "col2",
Character( 1 ),
"Nominal",
Set Values( {"x", "x", "x", "y", "y", "y", "y", "z", "z", "z"} ),
Set Display Width( 45 )
)
);
// create the new column
dt << New Column( "col3b" );
:col3b[N Rows( dt )] = :col1[N Rows( dt )];
For( i = N Rows( dt ) - 1, i >= 1, i--,
If( :col2[i + 1] != :col2[i],
:col3b[i] = :col1[i],
:col3b[i] = :col3b[i + 1] * :col1[i]
)
);
嗨吉姆@tx尼尔森
这实际上适用于较小的表(即数千行)。 谢谢。
我遇到的“新”问题是我的数据表非常大。 当我将公式嵌入新列并单击“应用”时,程序挂起,我不得不杀死 jmp 才能退出。
当我将鼠标悬停在公式上时,这是有关“当前数据表”的警告消息。
“当前的数据 table() 有副作用,通常不适合在公式中使用。 如果这是必要的,您可以使用 expr 或 eval 来防止在公式中执行......”
我的脚本能力非常有限。 感谢您对此主题的进一步帮助!
问候,
廷慧
@tx尼尔森写道:
这是新的公式……它与上一个公式的方法相同,但只需找到每个组的起点和终点即可。
If( Row() == 1,
curCol = :col2;
end = Max( Current Data Table() << get rows where( :col2 == curCol ) );
x = :col1;
,
:col2 != curCol;
curCol = :col2;
end = Max( Current Data Table() << get rows where( :col2 == curCol ) );
x = :col1;
);
For( i = Row() + 1, i <= end, i++,
x = x * :col1[i]
);
x;
这是新的公式……它与上一个公式的方法相同,但只需找到每个组的起点和终点即可。
If( Row() == 1,
curCol = :col2;
end = Max( Current Data Table() << get rows where( :col2 == curCol ) );
x = :col1;
,
:col2 != curCol;
curCol = :col2;
end = Max( Current Data Table() << get rows where( :col2 == curCol ) );
x = :col1;
);
For( i = Row() + 1, i <= end, i++,
x = x * :col1[i]
);
x;

嗨吉姆@tx尼尔森
感谢您的回复。
不,我的列名称没有改变。 它们保持为给定的 col1、col2。我想添加一个新列 (col3),它对 col2 中给出的相同值进行向后乘法。 我在下表中展示了我的需求。能否请你帮忙?
列1 列2 列3
1 x 6(即1 x 2 x 3)
2 x 6(即2 x 3)
3×3(即3)
2 y 240(即2x4x5x6)
4×120(即4×5×6)
5 y 30(即 5 x6)
6 y 6(即6)
7z 504(即7x8x9)
8 z 72(即8x9)
9 9
谢谢!
由于我错过了分组变量,我删除了我的帖子。 我的方法在这种情况下不起作用。
我不明白你的三组专栏。看起来好像您正在更改数据表中间的列名称,这在 JMP 中不起作用。此表格格式是来自 Excel 电子表格吗?
这是一个简单的公式,可以满足您对数据表的需求,如下所示

我附上了数据表。
使用的公式是:
x = :col2[Row()];
If( Row() < N Rows( Current Data Table() ),
For( i = Row() + 1, i <= N Rows( Current Data Table() ), i++,
x = x * :col2[i]
)
);
x;
如果数据表中有多个位置需要重新开始乘法,则可以使用相同的想法。只需确定何时重新启动和何时结束即可