How to Convert a Factor into a Date in R

When you need to convert factor to date in r there is a simple function that does the conversion for you. In r programming, if you want to process factor dates as the date data type you will need to convert factor vectors into date type vectors. Once your vector is a date type, you can process the data as dates.

Description – Convert a Factor into a Date in R

When you convert a factor to a date in r you use the date function which has the format of as.Date(vector, format). “Vector” is the vector being converted and “format” is the format of the date as stored in the factor. The date function is a base r function that converts a properly formatted data type into a date. You can use this technique to create a date vector from an r data frame that has a factor column containing its dates. It is a straightforward process, but you need to get the format right for it to work.

How It Works

To convert a factor to a date in r, use the date function to convert the factor object into a date object. This process will also work with a character vector, but you will get an error message if you try it with a numeric vector. The date function in r is similar to the strptime function in Python but it is on at a different level because it also works with factors. This process allows you to process data originally stored as a factor as the date it represents. The categorical nature of factor vectors is the reason you would store a date as a factor rather than a date, particularly when dealing with data frames.

Examples: Convert a Factor into a Date in R

Here are three examples where we convert a factor to a date. Each example uses a different date format in the factor vector.

> d1 = factor(c(“1961-04-21”,
+ “1968-12-24”,
+ “1969-07-20”,
+ “1981-04-21”,
+ “1986-01-28”,
+ “2003-02-01”,
+ “2020-11-16”,
+ “2021-09-15”))
> d1
[1] 1961-04-21 1968-12-24 1969-07-20 1981-04-21 1986-01-28 2003-02-01 2020-11-16 2021-09-15
Levels: 1961-04-21 1968-12-24 1969-07-20 1981-04-21 1986-01-28 2003-02-01 2020-11-16 2021-09-15
> class(d1)
[1] “factor”
> d2 = as.Date(d1, format = “%Y-%m-%d”)
> d2
[1] “1961-04-21” “1968-12-24” “1969-07-20” “1981-04-21” “1986-01-28” “2003-02-01” “2020-11-16” “2021-09-15”
> class(d2)
[1] “Date”
> as.Date(d1)
[1] “1961-04-21” “1968-12-24” “1969-07-20” “1981-04-21” “1986-01-28” “2003-02-01” “2020-11-16” “2021-09-15”

In this example, we use the date format of year-month-day, and it works fine when the format argument is included. It also works fine when the format argument is not included because this is the default format.

> d1 = factor(c(“21-04-1961”,
+ “24-12-1968”,
+ “20-07-1969”,
+ “21-04-1981”,
+ “28-01-1986”,
+ “01-02-2003”,
+ “16-11-2020”,
+ “15-09-2021”))
> d1
[1] 21-04-1961 24-12-1968 20-07-1969 21-04-1981 28-01-1986 01-02-2003 16-11-2020 15-09-2021
Levels: 01-02-2003 15-09-2021 16-11-2020 20-07-1969 21-04-1961 21-04-1981 24-12-1968 28-01-1986
> class(d1)
[1] “factor”
> d2 = as.Date(d1, format = “%d-%m-%Y”)
> d2
[1] “1961-04-21” “1968-12-24” “1969-07-20” “1981-04-21” “1986-01-28” “2003-02-01” “2020-11-16” “2021-09-15”
> class(d2)
[1] “Date”
> as.Date(d1)
[1] “0021-04-19” “0024-12-19” “0020-07-19” “0021-04-19” “0028-01-19” “0001-02-20” “0016-11-20” “0015-09-20”

In this example, we use the date format of day-month-year, and it works fine when the format argument is included. When the format argument is not included, we do not get an error message, but it messes up the dates.

> d1 = factor(c(“04-21-1961”,
+ “12-24-1968”,
+ “07-20-1969”,
+ “04-21-1981”,
+ “01-28-1986”,
+ “02-01-2003”,
+ “11-16-2020”,
+ “09-15-2021”))
> d1
[1] 04-21-1961 12-24-1968 07-20-1969 04-21-1981 01-28-1986 02-01-2003 11-16-2020 09-15-2021
Levels: 01-28-1986 02-01-2003 04-21-1961 04-21-1981 07-20-1969 09-15-2021 11-16-2020 12-24-1968
> class(d1)
[1] “factor”
> d2 = as.Date(d1, format = “%m-%d-%Y”)
> d2
[1] “1961-04-21” “1968-12-24” “1969-07-20” “1981-04-21” “1986-01-28” “2003-02-01” “2020-11-16” “2021-09-15”
> class(d2)
[1] “Date”
> as.Date(d1)
Error in charToDate(x) :
character string is not in a standard unambiguous format

In this example, we use the date format of month-day-year, and it works fine when the format argument is included. When the format argument is not included, we get an error message, because the function is trying to read a day as a month, and there are day values that are greater than twelve.

Application: Convert a Factor into a Date in R

There are some interesting applications to converting a factor to a date in an r program. One application is when you have a time series stored as a factor and you want to arrange the dates with the earlier dates first and the later ones last. Another application is when you have the time date column of a data frame, and you want to process the dates in that column as dates. You would want to store the dates in the data frame as a factor because they are associated with the contents of other columns which makes having them stored as a factor vector convenient. However, there are going to be times when you are going to want to process the dates using functions that require the date data type. When this is the case, it is necessary to convert the factor vector into a date vector so that you can process it as a date type. Each of these main applications has multiple sub-applications to them, as a result converting a factor to a date has many applications.

When you convert a factor to a date in r, it is a straightforward process but one you must be careful with. You must have the format of the date within the factor correct, otherwise, it will mess up the dates or give you an error message.

Scroll to top