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.
There are several ways to check data type in R. We can make use of the “typeof()” function, “class()” function and even the “str()” function to check the data type of an entire dataframe.
In addition to the time data types R also has a date data type. The difference is that the date data type keeps track of numbers of days rather than seconds. You can cast a string into a date type using the as. Date function.
To get the current system date, we can use the Sys. Date() function. Sys.
The previous R syntax has created a new vector object called my_dates_updated, which looks exactly the same as the original character vector. Our new vector object has the Date class! Now, we can apply the min and max functions to find the earliest and latest dates of our vector.
Use inherits
to detect if argument has datatype Date
:
is.date <- function(x) inherits(x, 'Date') sapply(list(as.Date('2000-01-01'), 123, 'ABC'), is.date) #[1] TRUE FALSE FALSE
If you want to check if character argument can be converted to Date
then use this:
is.convertible.to.date <- function(x) !is.na(as.Date(as.character(x), tz = 'UTC', format = '%Y-%m-%d')) sapply(list('2000-01-01', 123, 'ABC'), is.convertible.to.date) # [1] TRUE FALSE FALSE
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With