如何更改数据表以将设置为 0 的值转换为点,我找到了将点更改为零但没有替代方案的脚本。
谢谢,
好时机。 上周我只是在处理一些相关的代码,减少了一些执行时间,想补充一下已经说过的内容。在一个脚本中从遍历行并替换为矩阵方法(Vince 在上面提到的方法)将执行时间缩短了大约 20 到 25 倍。在另一个脚本上,从获取行 where() 更改为矩阵方法将其改进了近 2 倍。 在做的过程中,我写了下面的代码,以便以后分享或参考,在这里发布似乎也很合理。 请注意,将 Loc() 与来自字符列的列表一起使用会使用不同的语法,并且您无法像使用从数字列获得的全矩阵变量那样构建更复杂的逻辑。 所以我使用了 Craige Hales 分享的技术(链接在这里) 使用关联数组和交集函数。 希望你觉得它有用。
请注意,此脚本添加了新的字符列并在其中添加了注释,因为我编写它是为了演示目的。 鉴于此处的原始问题,您可以只替换分配行,例如c3[rowsMale] = "男性";和列(dtData,“年龄”) [rowsMale] = .;或类似的。
names default to here(1);
dtData = Open( "$SAMPLE_DATA/Big Class.jmp" );
mAge = column(dtData, "age") << get values(); //a matrix
mHeight = column(dtData, "height") << get values(); //a matrix
lSex = column(dtData, "sex") << get values(); //a list
lName = column(dtData, "name") << get values(); //a list
<BR />//note the results of the Loc() function will return a matrix that corresponds to the rows that match the criteria<BR />
rowsTall = Loc(mHeight > 63); // simple comparison in Loc() function.
c1 = new column("Comments 1","Character");
c1[rowsTall] = "Tall";
rowsTallAndOver13 = Loc( (mHeight > 63) & (mAge > 13)); // can build more complex logic in Loc() function
c2 = new column("Comments 2","Character");
c2[rowsTallAndOver13] = "Tall and 14 or older";
rowsMale = Loc(lSex, "M"); // note syntax is slightly different with Loc() for lists when dealing with character columns.
//rowsMaleNamedRobert = Loc((lSex, "M"), (lName, "Robert")); // Cannot build complex comparisons with Character columns, see below on the intersection and AA's
c3 = new column("Comments 3","Character");
c3[rowsMale] = "Male";
//rowsTall17M = Loc((mHeight > 63) & (mAge == 17) & (lSex == "M") ); // attempt at building complex logic but the syntax is not correct, and can't have lists in a compound comparison
//rowsTall17M = Loc((mHeight > 63) & (mAge == 17) & (lSex, "M") ); // attempt at building complex logic. Syntax is closer to correct, but can't have lists in a compound comparison<BR />//Build numeric comparisons all at once, then do character comparisons one at a time. Only 1 character comparison shown here.
rowsTall17 = Loc((mHeight > 63) & (mAge == 17) ); // Build the numeric parts separate from char column(s)
rowsMale = Loc(lSex, "M"); // Character columns separately. Seems you need one variable per character column, and then intersect them all later.
<BR />//insert above into associative arrays, use intersection function to get the matrices intersection
aa_rowsTall17 = associative array(rowsTall17);
aa_rowsMale = associative array(rowsMale);
intersection = aa_rowsMale; // copy of aa_rowsMale, as intersection gets modified in next line
intersection<<INTERSECT> 63) );
c5 = new column("Comments 5 - using get rows where","Character");
c5[gRowsTallM17] = "Tall, Male, 17";</INTERSECT>
如果你想做整张桌子,你可以做
Names default to here( 1 );
dt = open("$SAMPLE_DATA\Big Class.jmp");
dt:weight[[1, 5, 6, 8]] = 0; // just setting them to 0
dt:height[[11, 15, 16, 18]] = 0;
wait(2);
// this is the bit you'll want
numeric_cols = dt << Get Column Names("Numeric", "string");
m = dt[0, numeric_cols]; // get the values as a matrix;
m[Loc(m==0)]=.; // set them to missing
dt[0, numeric_cols] = m; // set the data to the matrix对于名为“A”的列,这是将所有“0”转换为缺失值的一种方法;
Names Default To Here( 1 );
dt = Current Data Table();
theRows = dt << get rows where( :A == 0 );
Try( :A[theRows] = . );
我建议看看这篇文章您应该循环遍历数据表还是使用重新编码,或者使用获取行在哪里更改数据表中的值(和原来的邮政) 和那里的讨论。 如果我没记错的话,那一点就是用缺失值替换 999。 基本思想通常是遍历行或列并替换值。