jeffrey jeffrey - 4 months ago 17
R Question

Is there a way to check if a column is a Date in R?

This is a pseudo followup to this question: Why is ggplot graphing null percentage data points?

Let's say this is my dataset:

Date AE AA AEF Percent
1/1/2012 1211 1000 3556 0.03
1/2/2012 100 2000 3221 0.43
1/3/2012 3423 10000 2343 0.54
1/4/2012 10000 3000 332 0.43
1/5/2012 2342 500 4435 0.43
1/6/2012 2342 800 2342 0.23
1/7/2012 2342 1500 1231 0.12
1/8/2012 111 2300 333
1/9/2012 1231 1313 3433
1/10/2012 3453 5654 222
1/11/2012 3453 3453 454
1/12/2012 5654 7685 3452

> str(data)
'data.frame': 12 obs. of 5 variables:
$ Date : Factor w/ 12 levels "10/11/2012","10/12/2012",..: 1 2 3 4 5 6 7 8 9 10 ...
$ AE : int 1211 100 3423 10000 2342 2342 2342 111 1231 3453 ...
$ AA : int 1000 2000 10000 3000 500 800 1500 2300 1313 5654 ...
$ AEF : int 3556 3221 2343 332 4435 2342 1231 333 3433 222 ...
$ Percent: num 0.03 0.43 0.54 0.43 0.43 0.23 0.12 NA NA NA ...


I need something to tell that the 'Date' column is a Date type as opposed to a numeric or character type (this is because I have to convert the 'Date' column of the data input into an actual Date with as.Date(), ASSSUMING that I do not know the column names of the data set).

is.numeric(data[[1]]) returns False
is.character(data[[1]]) returns False


I made the 'Date' column in Excel, formatting the column in the 'Date' format, then saved the file as a csv. What type is this in R? I seek an expression similar to the above that returns TRUE.

Answer

You could try to coerce all the columns to as.Date and see which ones succeed. You would need to specify the format you expect dates to be in though. E.g.:

data <- data.frame(
  Date=c("10/11/2012","10/12/2012"),
  AE=c(1211,100),
  Percent=c(0.03,0.43)
)

sapply(data, function(x) !all(is.na(as.Date(as.character(x),format="%d/%m/%Y"))))
#Date      AE Percent 
#TRUE   FALSE   FALSE