如何从具有'?'的数据框中替换或删除数据?在R中吗?

问题描述 投票:0回答:1

我有一个数据集,其中很少有数据是“?”(请参见下图以供参考)

workclass有一个“?”在此示例数据中

age         workclass fnlwgt     education education_num         marital_status
39         State-gov  77516     Bachelors            13          Never-married
31           Private  45781       Masters            14          Never-married
42           Private 159449     Bachelors            13     Married-civ-spouse
30           Private 188146       HS-grad             9     Married-civ-spouse
30           Private  59496     Bachelors            13     Married-civ-spouse
44           Private 343591       HS-grad             9               Divorced
44           Private 198282     Bachelors            13     Married-civ-spouse
32      Self-emp-inc 317660       HS-grad             9     Married-civ-spouse
17                 ? 304873          10th             6          Never-married
28           Private 377869  Some-college            10     Married-civ-spouse
38  Self-emp-not-inc 120985       HS-grad             9     Married-civ-spouse
40       Federal-gov  56795       Masters            14          Never-married

sample of my dataset我试过过滤器,在哪里和其他几个匹配功能,但它不能捕获?以及字符串或整数。

我是R语言的新手,无法解决此问题。

我想获取包含“?”的数据计数然后根据计数决定删除行或用一些有意义的数据填充它。

r filter data-cleaning
1个回答
0
投票

假设您的数据帧称为“ df”,则可以通过运行:sum(df$workclass == "?")]获得计数。

我会考虑将这些值转换为适当的NA值,例如通过运行:ifelse(df$workclass == "?", NA, df$workclass)

[一旦将它们转换为NA后,您可以例如通过na.omit(df)将其删除,或者可以使用诸如模式插补或KNN输入等插补技术。您可以在此处阅读有关插补技术和缺失值处理的更多信息:https://towardsdatascience.com/all-about-missing-data-handling-b94b8b5d2184

© www.soinside.com 2019 - 2024. All rights reserved.