We provide effective and economically affordable training courses for R and Python, Click here for more details and course registration !
R has rich resources of functions dealing with date values in data analysis. In this post, we introduce and show how to use as.Date() function for working with data values in R. as.Date() accepts a string input with specific format and transform the value to a date object in R. The basic form of the function is as.Date(x, “input_format”), where x is the string input, and the “input_format” tells which kind of date information the input string specifies. The following table shows the abbreviation of alphabet representation for date format in R.
The following code example shows inputting and transforming two strings as date objects. The default format for accepted date format is “year-month-day”.
#transform two strings into date format "year-month-day"
#using the default format here
testdates <- as.Date(c("2008-09-21", "2009-01-16"))
> testdates
[1] "2008-09-21" "2009-01-16"
The following example shows first reading a csv file into a data frame from working directory, then transform the data frame’s column “Date” as date format.
#to set working directory
setwd("d:\\RStatistics-Tutorial")
#read a csv file into data frame "grade"
vartype<-c("character", "character", "character", "character", "character", "numeric","numeric", "numeric","numeric","character")
grade <- read.table("University-NA.csv", colClasses=vartype, header=TRUE, sep=",")
grade
grade$Date
#output
[1] "10/31/08" "03/16/08" "05/22/08" "01/24/09" "07/30/09" "04/05/09"
[7] "11/24/08" "10/02/08" "06/05/09" "08/01/08" "10/24/08" "11/08/09"
[13] "10/29/08" "10/24/08" "10/15/08" "03/11/09" "05/24/08" "07/09/09"
[19] "08/12/09" "999"
#transform "Date" variable of data frame into date format
#using the accepted string format is "month/day/year"
grade$Date<- as.Date(grade$Date, "%m/%d/%y")
grade$Date
#output
[1] "2008-10-31" "2008-03-16" "2008-05-22" "2009-01-24" "2009-07-30"
[6] "2009-04-05" "2008-11-24" "2008-10-02" "2009-06-05" "2008-08-01"
[11] "2008-10-24" "2009-11-08" "2008-10-29" "2008-10-24" "2008-10-15"
[16] "2009-03-11" "2008-05-24" "2009-07-09" "2009-08-12" NA
Other two important functions for date and time are Sys.Date() and date().
Sys.Date() returns today’s date, and date() shows the current date and time.
#returns today’s date
Sys.Date()
[1] "2024-03-31"
#shows the current date and time
date()
[1] "Sun Mar 31 17:52:29 2024"
When date object is created, format() lets you output a existing date object’s value in a specific format. The examples below show how to carry out these operations.
#create a date object "today"
today <- Sys.Date()
#output date value in format "mon day year"
format(today, format="%b %d %y")
[1] "Mar 31 24"
#output date value in format "weekday"
format(today, format="%A")
[1] "Sunday"
Arithmetic operations can be performed on date objects. Following code example shows difference between two date values, in number of days.
#create two date objects
date1 <- as.Date("2024-01-16")
date2 <- as.Date("2023-07-29")
#difference, in days, between two date objects
days <- date2 - date1
days
Function difftime() allows calculation of date difference in specified units.
#calculate the time difference between date objects in weeks
> difftime(date1, date2, units="weeks")
#output
Time difference of 24.42857 weeks
#calculate the time difference between date objects in minutes
> difftime(date1, date2, units="mins")
#output
Time difference of 246240 mins
You can also watch R tutorial full video on our YouTube channel.
0 Comments