The “Date” class means dates are stored as the number of days since January 1, 1970, with negative values for earlier dates. They may appear to still be character data when printed, but they are in fact numbers. The dates now have class “Date” and are printed in year-month-day format. Below we generate two character vectors of dates, inspect their class, reformat them using the mdy function and then inspect their class again. lubridate provides functions for every permutation of “m”, “d”, “y”. If our dates were in the order of, say, year-month-day, we would use the ymd function. Therefore we would use the mdy function to transform the column to a date object. For example, if our data has a column of dates such as May 11, 1996, our dates are ordered month-day-year.
#Median date calculator series#
To format dates, lubridate provides a series of functions that are a permutation of the letters “m”, “d” and “y” to represent the ordering of month, day and year. If dates or times are stored as character or factor that means we can’t calculate or summarize elapsed times.
(Though see the readr package for functions that attempt to parse date and times automatically.) Using the str or class functions will tell you how they’re stored. When we import data into R, dates and times are usually stored as character or factor by default due to symbols such as “-”, “:” and “/”. The out-of-the-box base R installation also provides functions for working with dates and times, but the functions in the lubridate package are a little easier to use and remember. The lubridate package for the R statistical computing environment was designed to help us deal with these kinds of data.
What was the difference in times between subjects that received the treatment and those that did not? If our data is stored and read in as something like “01:23:03”, then we’ll need to convert to seconds. How many days was the person in the study? (Don’t forget 2008 was a leap year February had 29 days.) What was the median time-in-study for all subjects?Īnother example are experiments that time participants performing an activity, applies a treatment to certain members, and then re-times the activity. A subject may enter a study on and exit on November 4, 2009. A common example in the health sciences is time-in-study. Sometimes we have data with dates and/or times that we want to manipulate or summarize.