0

I've looked all around for this, but have found no answers. I have a data frame that contains columns with multiple levels along the lines of "Unknown" "No response" or "Refused to answer" and the like. All of these are useless to me for analysis, so I want to replace them all with NA.

Note that I do not want to replace them across the entire data frame, only specific columns! There are other columns that contain values with the same names that are actually useful to me and I want to leave them alone.

I've managed to replace them one at a time by using:

data$col1 <- factor(gsub("Unknown", "NA", data$col1))

but that only works for one string at a time. If I try to add multiple strings, R throws an error. Is there a more efficient way to do this?

I'm relatively new to coding, please be gentle!

2
  • Use the na.strings in read.csv i.e. while reading the dataset, you can specify which values can be changed to NA, dat <- read.csv("yourfile.csv", na.strings = c("Unknown", "No response", "Refused to answer")) Commented Dec 4, 2016 at 3:40
  • Try data$col1 <- factor(gsub("Unknown|No response|Refused to answer", "NA", data$col1)). Commented Dec 4, 2016 at 3:43

1 Answer 1

1

If we need to change multiple values to NA, one option is using na.strings in read.csv/read.table while reading the data

dat <- read.csv("yourfile.csv", na.strings = c("Unknown", "No response", 
             "Refused to answer"))

However, here the problem is with specific columns, in that case, create an index of the columns, loop through the columns and replace the values by creating a logical index with %in% (assuming that these are not substrings)

columnsOfInterest <- c(1, 4, 5) #just for an example
df1[columnsOfInterest] <- lapply(df1[columnsOfInterest], function(x)
         replace(x, x %in% c("Unknown", "No response", "Refused to answer"), NA))

NOTE: changing to quoted NA i.e. "NA" is rather useless, instead we need just NA

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.