To convert a Character (string) column to a Date column in R DataFrame:
df$column_name <- as.Date(df$column_name)
Example of Converting Character to Date Column in R DataFrame
Here is an example of converting a Character column to a Date column in R DataFrame, where the date format is “yyyy-mm-dd” (additional date formats are available in the section below):
# Create a DataFrame df <- data.frame( date_string = c("2023-07-15", "2023-08-20", "2023-09-10", "2023-10-05", "2023-05-11") ) # Display the original DataFrame print("Original DataFrame:") print(df) # Print the data types before conversion print("Data Types Before Conversion:") str(df) # Convert the string column to date df$date <- as.Date(df$date_string) # Display the new DataFrame print("New DataFrame:") print(df) # Print the data types after conversion print("Data Types After Conversion:") str(df)
The result is a new date column, where the data type is Date:
[1] "Original DataFrame:"
date_string
1 2023-07-15
2 2023-08-20
3 2023-09-10
4 2023-10-05
5 2023-05-11
[1] "Data Types Before Conversion:"
'data.frame': 5 obs. of 1 variable:
$ date_string: chr "2023-07-15" "2023-08-20" "2023-09-10" "2023-10-05" ...
[1] "New DataFrame:"
date_string date
1 2023-07-15 2023-07-15
2 2023-08-20 2023-08-20
3 2023-09-10 2023-09-10
4 2023-10-05 2023-10-05
5 2023-05-11 2023-05-11
[1] "Data Types After Conversion:"
'data.frame': 5 obs. of 2 variables:
$ date_string: chr "2023-07-15" "2023-08-20" "2023-09-10" "2023-10-05" ...
$ date : Date, format: "2023-07-15" "2023-08-20" ...
Note that the str(df) function was used to print the data types of the column before and after the conversion.
Other Date Formats
(1) Date format of “dd-mm-yyyy”
df <- data.frame( date_string = c("15-07-2023", "20-08-2023", "10-09-2023", "05-10-2023", "25-11-2023") ) df$date <- as.Date(df$date_string, format = "%d-%m-%Y") print(df) str(df)
Output:
date_string date
1 15-07-2023 2023-07-15
2 20-08-2023 2023-08-20
3 10-09-2023 2023-09-10
4 05-10-2023 2023-10-05
5 25-11-2023 2023-11-25
'data.frame': 5 obs. of 2 variables:
$ date_string: chr "15-07-2023" "20-08-2023" "10-09-2023" "05-10-2023" ...
$ date : Date, format: "2023-07-15" "2023-08-20" ...
(2) Date format of “dd/mm/yyyy”
df <- data.frame( date_string = c("15/07/2023", "20/08/2023", "10/09/2023", "05/10/2023", "25/11/2023" ) df$date <- as.Date(df$date_string, format = "%d/%m/%Y") print(df) str(df)
Output:
date_string date
1 15-07-2023 2023-07-15
2 20-08-2023 2023-08-20
3 10-09-2023 2023-09-10
4 05-10-2023 2023-10-05
5 25-11-2023 2023-11-25
'data.frame': 5 obs. of 2 variables:
$ date_string: chr "15-07-2023" "20-08-2023" "10-09-2023" "05-10-2023" ...
$ date : Date, format: "2023-07-15" "2023-08-20" ...
(3) Date format of “mm.dd.yyyy”
df <- data.frame( date_string = c("07.15.2023", "08.20.2023", "09.10.2023", "10.05.2023", "11.25.2023") ) df$date <- as.Date(df$date_string, format = "%m.%d.%Y") print(df) str(df)
Output:
date_string date
1 07.15.2023 2023-07-15
2 08.20.2023 2023-08-20
3 09.10.2023 2023-09-10
4 10.05.2023 2023-10-05
5 11.25.2023 2023-11-25
'data.frame': 5 obs. of 2 variables:
$ date_string: chr "07.15.2023" "08.20.2023" "09.10.2023" "10.05.2023" ...
$ date : Date, format: "2023-07-15" "2023-08-20" ...