按年齡列對jm進行排序後,添加一個新列,同時查找相鄰行的年齡,同時不返回年齡1.我嘗試循環並感覺自己慢。請求更好的方法。謝謝你非常喜歡!
dt = Open(“$ SAMPLE_DATA / Big Class.jmp”);
新列(“新”,數字,連續,寬度(8));
dt的[1,“年齡”] = 1;
for(i = 1,i <= Col Number(1),i ++,
如果(DT [I,“年齡”] == dt的[I-1,“年齡”],dt的[I,“新”] = 0,dt的[I,“新”] = 1);
);

請注意,您粘貼的腳本與屏幕截圖中的腳本不匹配。
Names Default to Here(1);
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt << Sort( By( :age ), Order( Ascending ), Replace Table );
dt << New Column("new",Numeric,Continuous,Width(8));
t0 = hptime();
dt[1,"new"]=1;
for(i=2,i<=Col Number(1),i++,
if(dt[i,"age"]==dt[i-1,"age"],dt[i,"new"]=0,dt[i,"new"]=1);
);
t1 = hptime();
dt << New Column("new2",Numeric,Continuous,Width(8));
t2 = hptime();
column(dt, "new2") << set each value(
If(row() == 1, 1,
:age == Lag(:age,1), 0,
//else
1)
);
t3 = hptime();
show(t3-t2, t1-t0); <BR /><BR />//The results will be different each time, but the second method takes less than 50% of the time <BR />//compared to the for loop.<BR />//t3 - t2 = 257; t1 - t0 = 679;<BR /><BR /><BR />