0

In the test dataframe below, I am attempting to change every string in the dataframe containing "NA" to "" (so as to make NAs blank).

dat <- as.data.frame(matrix(ncol=2, nrow=2))
dat$V1 <- c("  NA", "foo")
dat$V2 <- c("bar", "NA   ")

dat
   V1   V2
1  NA  bar
2 foo NA 

However, the following command returns a completely blank dataframe, as if all strings contained "NA". Why does this happen and what would be the correct solution?

value <- "NA"

dat[grepl(value, dat)] <- ""
2
  • You are replacing by column here... Commented Aug 24, 2017 at 18:03
  • To explain Damiano's comment further, you're replacing columns with "" if they contain "NA". Since every column contains "NA", you are of course left with a blank dataframe. Note the difference if you construct a dataframe with dat$V1 <- c(" NA", "NA ") and dat$V2 <- c("foo", "bar ") Commented Aug 24, 2017 at 18:10

4 Answers 4

1

Just using gsub

value <- "NA" 

for (i in 1:ncol(dat)) {
  dat[,i] <- gsub(value, "", dat[,i])  
}
dat
Sign up to request clarification or add additional context in comments.

Comments

1
dat <- lapply(dat, function(x) {gsub("NA", "", x)})
dat <- data.frame(dat)

Comments

0
library(data.table)
setDT(dat)

for(j in seq_along(dat)){
  set(dat, i = which(dat[[j]] %like% "NA"), j = j, value = "")
}
      V1  V2
# 1:     bar
# 2: foo  

Comments

0

Maybe in your case you are better off with a matrix.

datm <- as.matrix(dat)

Now your proposed solution works:

datm[grepl(value, datm)] <- ""

or using gsub:

datm = gsub("\\s*NA\\s*", "",datm)

You can convert it to a dataframe after data cleansing.

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.