早上好,
有人可以帮助我吗? ...
我想用替换数字列的空值平均列值。
我试过了
dt =当前数据表();
填充 = dt <<获取列名(数字);
为了( 我 = 1个, 我 <= N 项( 填充 ), i++,
填充[i][dt <<获取行在哪里(不见了( 填充 [i][] ) )] =均值(填充[i]);
);
提前致谢
詹保罗
我没有详细研究您的代码,但感觉您所做的工作超出了您的需要。 为了使事情更具可读性,您可以考虑使用如下函数:
NamesDefaultToHere(1);
// Only impute if more than n cells are missing
imputeContinuousCol =
Function({col, n}, {Default Local},
values = col << getValues;
missingValuePos = Loc(IsMissing(values));
if(NRow(missingValuePos) > n,
values[Loc(IsMissing(values))] = Mean(values);
col << setValues(values);
);
);
// Only impute if more than n cells are missing
imputeCharacterCol =
Function({col, n}, {Default Local},
values = col << getValues;
missingValuePos = Loc(values, "");
if(NRow(missingValuePos) > n,
values[Loc(values, "")] = "NA";
col << setValues(values);
);
);
// Data table . . .
dt = DataTable("Big Class.jmp");
// List of column names that satidfy your imputation criteria . . .
imputeList = {"name", "sex", "height"};
// Loop over this list, and impute if necessary
for (c=1, c<=NItems(imputeList), c++,
col = Column(dt, imputeList[c]);
if(
(col << getModelingType) == "Continuous",
imputeContinuousCol(col, 1),
(col << getDataType) == "Character",
imputeCharacterCol(col, 1),
);
);
打开“Big Class”,在列出的列中缺少一些单元格,然后尝试一下。
嗨,伊恩
首先感谢您的帮助和建议。你的脚本工作得很好
所以.... 我试图将您的脚本集成到一个更大的脚本中,以减少和简化数据库。
这里是整个工作
名称默认为此处(1);
清除全局变量();
// 用于在所有打开的表中选择哪一个表进行分析的代码
OpenDT = 列表();
For( i = 1, i <= N 表(), i++,
插入(OpenDT,数据表(i))
);
dlgDT = Dialog( "选择要分析的数据表", VList( selectDT = List Box( OpenDT ) ), HList( Button( "OK" ), Button( "Cancel" ) ) );
显示(dlgDT);
如果(dlgDT[“按钮”] == 1,
删除自(dlgDT,N 项(dlgDT));
评估列表(dlgDT);
);
当前数据表( selectDT[1] );
// ----------------------------------------GUI ------ ----------------------------------//
cd = 列对话框(
Title("数据预处理"),
List= ColList( "选择列 >> ", MinCol( 1 ) ),
HList( " % of Empty elements in the column -> (0-100) ", mypercent1 = EditNumber( 50 ) ),
HList( " % of Stagnant elements in the column -> (0-100) ", mypercent = EditNumber( 80 ) ),
列表(
numeric = Check Box( "Columns Numeric - Impute missing cells with the mean of the others", 1),
character = Check Box( "Columns character - Impute missing cells with N/A", 1 ),
),
);
//变量声明
impute_num_col = cd["数字"]; // 估算数字
impute_char = cd["字符"]; // 估算字符
百分比 = cd["我的百分比"];
percent1 = cd["mypercent1"];
mylist = cd["列表"];
dt=选择DT[1];
delcols = {};
numRows = nrows(dt);
for(i=1; maxi=Nitems(mylist), i<=maxi, i++,
如果(nrows(loc(aslist(column(dt,mylist[i])<= numRows * percent/100,
插入(delcols,myList[i]);
,
if(Col N Missing(column(dt,mylist[i])) >= numRows * percent1/100,
插入(delcols,myList[i]);
);
);
);
nitems = N 项(delcols);
列名 = {};
colnamelist = {};
对于 ( p = 1, p <= nitems, p++,
colname = Column( delcols[p] ) << 获取名称;
插入(colnamelist,colname(p));
);
dt << 删除列(colnamelist);
//好的! 列数字 - 用其他单元格的平均值估算缺失的单元格
如果(impute_num_col == 1,
cc = dt << 获取列名(连续);
ncols = N 项( cc );
对于 ( i = 1, i <= ncols, i++,
values = Column( dt, cc[i] ) << get values( 1 );
values[Loc( Is Missing( values ) )] = Mean( values );
Column( dt, cc[i] ) << setvalues( values, cc[i] );
);
);
// 列字符 - 用 NA 估算缺失的单元格
如果(impute_char == 1,
FILL = dt << 获取列名(字符);
For( i = 1, i <= N Items( FILL ), i++,
FILL[i][dt << get rows where( Is Missing( FILL[i][] ) )] = "NA";
);
);
但问题是脚本的最后一部分(#Columns character - Impute missing cells with NA#)
不知道为什么……不起作用……
很奇怪,因为如果没有集成到上面的工作中,相同的代码就可以工作。
dt = 当前数据表();
FILL = dt << 获取列名(字符);
For( i = 1, i <= N Items( FILL ), i++,
FILL[i][dt << get rows where( Is Missing( FILL[i][] ) )] = "NA";
);
你知道为什么会这样吗... ?
感谢合作回复:如何用平均列值替换数字列的空值请找到一种方法来做到这一点:
NamesDefaultToHere(1);
// Make some test data
dt = NewTable("Test", NewColumn("Data", Numeric, Continuous, Formula(if(Mod(Row(), 2) == 0, RandomNormal()))));
dt << addRows(20);
dt << runFormulas;
Column(dt, "Data") << deleteFormula;
// Impute missing cells with the mean of the others
Wait(3);
col = Column(dt, "Data");
values = col << getValues;
values[Loc(IsMissing(values))] = Mean(values);
col << setValues(values);
描绘通过为单元格着色而估算的值可能是一种很好的做法。