R date column error using data[data==""] <- NA

420 views Asked by At

I am working with a data set which has all kinds of column classes, including class "Date". I try to assign NA to all empty values in this data set the following way:

data[data==""] <- NA

Obviously the date column makes some problems here, because there is the following error:

Error in charToDate(x) : 
character string is not in a standard unambiguous format

I do not really know why this error occurs, since there are no empty values in the date column, so it should happen nothing there. The dates in the date column are in a standard format "%Y-%m-%d".

What is the problem here and how can I solve it?

1

There are 1 answers

2
akrun On BEST ANSWER

You can create a logical index to subset columns other than the 'Date' class, and use that to replace the '' with NA

 indx <- sapply(data, class)!='Date'
 data[indx][data[indx]==''] <- NA

It is the 'Date' class that is creating the problem. Another option would be to convert the data to matrix so that all the columns will be character.

 data[as.matrix(data)==''] <- NA

Or as suggested by @Frank (and using replace)

 data[indx] <- lapply(data[indx],  function(x) replace(x, which(x==''), NA))

data

 set.seed(49)
 data <- data.frame(Col1= sample(c('',LETTERS[1:3]), 10, replace=TRUE), 
      Col2=sample(c('',LETTERS[1:2]), 10, replace=TRUE), 
      Date=seq(as.Date('2010-01-01'),length.out=10, by='day'),
       stringsAsFactors=FALSE)