你好,
数据表中有许多数字列,并且所有列中都存在缺失值。
我只想选择其中的几个(考虑到我已经知道列名),并将其中缺失的值替换为零。
我写了如下代码,但是不起作用。
你能帮忙吗?
谢谢!
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;
);
);