Test Whether Data Frame Column has Date Class in R (Example Code)
This tutorial demonstrates how to test whether a variable has the Date data type in the R programming language.
Exemplifying Data
my_df <- data.frame(var1 = as.Date(c("2021-11-10", # Example data "2023-03-17", "2025-03-29", "2026-10-07")), var2 = 11:14, var3 = as.Date(c("2023-10-15", "2022-06-16", "2024-03-29", "2032-11-17")), var4 = c("2023-11-12", "2024-01-27", "2024-11-11", "2023-10-03"), var5 = letters[1:4]) my_df # var1 var2 var3 var4 var5 # 1 2021-11-10 11 2023-10-15 2023-11-12 a # 2 2023-03-17 12 2022-06-16 2024-01-27 b # 3 2025-03-29 13 2024-03-29 2024-11-11 c # 4 2026-10-07 14 2032-11-17 2023-10-03 d |
my_df <- data.frame(var1 = as.Date(c("2021-11-10", # Example data "2023-03-17", "2025-03-29", "2026-10-07")), var2 = 11:14, var3 = as.Date(c("2023-10-15", "2022-06-16", "2024-03-29", "2032-11-17")), var4 = c("2023-11-12", "2024-01-27", "2024-11-11", "2023-10-03"), var5 = letters[1:4]) my_df # var1 var2 var3 var4 var5 # 1 2021-11-10 11 2023-10-15 2023-11-12 a # 2 2023-03-17 12 2022-06-16 2024-01-27 b # 3 2025-03-29 13 2024-03-29 2024-11-11 c # 4 2026-10-07 14 2032-11-17 2023-10-03 d
Example: Identify Date Class in Data Frame Columns
sapply(my_df, is.Date) # Check for Date class # var1 var2 var3 var4 var5 # TRUE FALSE TRUE FALSE FALSE |
sapply(my_df, is.Date) # Check for Date class # var1 var2 var3 var4 var5 # TRUE FALSE TRUE FALSE FALSE