如何更改數據表以將設置為 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。 基本思想通常是遍歷行或列並替換值。