how do I remove question mark(?) from a data set in R

心不动则不痛 提交于 2019-12-06 06:49:11
JeremyS

look at gsub

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

edit: forgot to add fixed = TRUE

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

Here's an easy way to replace " ?" with NA in all columns.

# find elements
idx <- census == " ?"
# replace elements with NA
is.na(census) <- idx

How it works?

The command idx <- census == " ?" creates a logical matrix with the same numbers of rows and columns as the data frame census. This matrix idx contains TRUE where census contains " ?" and FALSE at the other positions.

The matrix idx is used as an index. The command is.na(census) <- idx is used to replace values in census at the positions in idx with NA.

Note that the function is.na<- is used here. It is not identical with the is.na function.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!