如何从R中的数据集中删除问号(?) [英] how do I remove question mark(?) from a data set in R

查看:595
本文介绍了如何从R中的数据集中删除问号(?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在分析UCI成人census数据.数据的每个缺失值都有问号(?).

Hello everyone I am analysing UCI adult census data. The data has question marks (?) for every missing value.

我想用NA替换所有问号.

我尝试过:

library(XML)
census<-read.csv("https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data",header=F,na.strings="?")
names(census)<-c("Age","Workclass","Fnlwght","Education","EducationNum","MaritalStatus","Occupation"   
  ,"Relationship" , "Race","Gender","CapitalGain","CapitalLoss","HoursPerWeek","NativeCountry","Salary"  )

table(census$Workclass)

                ?       Federal-gov         Local-gov      Never-worked           Private      Self-emp-inc 
             1836               960              2093                 7             22696              1116 
 Self-emp-not-inc         State-gov       Without-pay 
             2541              1298                14 

x

<-ifelse(census$Workclass=="?",NA,census$Workclass)
 table(x)
x
    1     2     3     4     5     6     7     8     9 
 1836   960  2093     7 22696  1116  2541  1298    14

但是没有用.

请帮助.

推荐答案

看看gsub

census$x <- gsub("?",NA,census$x, fixed = TRUE)

忘记添加fixed = TRUE

正如Richard指出的那样,这将捕获所有出现的?

As Richard pointed out, this will catch all occurrences of a ?

这篇关于如何从R中的数据集中删除问号(?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆