你好,
數據表中有許多數字列,並且所有列中都存在缺失值。
我只想選擇其中的幾個(考慮到我已經知道列名),並將其中缺失的值替換為零。
我寫瞭如下代碼,但是不起作用。
你能幫忙嗎?
謝謝!
dt = data table("Untitled 9");
col_list = {"A","B"};
For( i = 1, i <= N Items( col_list ), i++,
col_list[i][dt << get rows where( Is Missing( col_list[i][]) )] = 0);我過去使用過搜索/替換選項:
選擇要用零替換缺失的列
搜索 。並替換為 0,請務必選中“僅在選定列中搜索”選項。
我的方法將替換所有缺失值數字列。 如果您只想替換特定列表中的值,只需提供
col_list = {“A”,“B”}
代替我對 col_list 的定義。
這是一種方法:
dt = New Table( "Test Zeroes", Add Rows( 3 ),
New Column( "Column 1", Character, "Nominal", Set Values( {"a", "b", "c"} ) ),
New Column( "Column 2", Numeric, "Continuous", Format( "Best", 12 ),
Set Values( [1, ., 3] ) ),
New Column( "Column 3", Numeric, "Continuous", Format( "Best", 12 ),
Set Values( [., 2, .] ) )
);
col_list = dt << get column names(string, numeric);
For( i = 1, i <= N Items( col_list ), i++,
missing_rows = dt << get rows where (is missing(as column(dt, col_list[i])));
if (nrows(missing_rows) > 0,
column(dt, col_list[i])[missing_rows] = 0;
);
);