Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从具有';的数据帧中替换或删除数据';在R里?_R_Filter_Data Cleaning - Fatal编程技术网

如何从具有';的数据帧中替换或删除数据';在R里?

如何从具有';的数据帧中替换或删除数据';在R里?,r,filter,data-cleaning,R,Filter,Data Cleaning,我有一个数据集,其中很少有数据“?”(参考下图) workclass在此示例数据中有一个“?” age workclass fnlwgt education education_num marital_status 39 State-gov 77516 Bachelors 13 Never-married 31 Private 45781 Masters

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

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
我尝试过filter、where和其他一些匹配函数,但它没有捕获?也可以是字符串或int

我不熟悉R语言,无法找到解决方案

我想得到一个包含“?”的数据计数,然后根据计数决定删除行或用一些有意义的数据填充

更新:::

我的数据是“?”而不是“?”。 看不清 一旦我得到了那个信息,我就能处理它了。
这是人为错误,而不是我尝试的数据/代码:D

假设您的数据帧被称为“df”,您可以通过运行:
sum(df$workclass==“?”)


我会考虑将这些值转换成适当的NA值,例如通过运行:<代码> DF$WorkStaseP><强>删除:< /强>

修订的_df=df[-其中(df$workclass==“?”),]

替换:让我们用“a”来代替


df[which(df$workclass==“?”,“workclass”]=“a”

这是否回答了您的问题
i
dplyr
filter(df,workclass==“?”)
或在
filter
内部使用
grepl
刚刚尝试了此方法来转换?但它将字符串值从------State gov Self emp not inc Private Federal gov[5]Local gov更改为NA?Self emp inc无薪[9]从未工作过--------------------到-------------------[1]8 7 5 2 3 NA 6 9 4----------我只是在转换前后运行unique?对于NAI,我不确定为什么这样做不起作用,但您也可以尝试:
df$workclass[df$workclass==”?“]