Coercing NAMCS data into date, date-time, and time objects
Author
Ella Rasmussen
Published
March 5, 2026
1Function(s) Name(s)
The functions being presented are as_datetime(), as_date(), and as_hms() from the lubridate package.
2 What is it for?
In health research, dates and visit times are often imported as character strings or numeric values that R cannot use for calculations in their raw form.
as_date(): Converts strings or numbers into a “Date” object (YYYY-MM-DD), which is the standard format for tracking days and years.
as_datetime(): Coerces data into a “POSIXct” object, which includes both the calendar date and the precise clock time (Hours:Minutes:Seconds).
as_hms(): Specifically handles the time-of-day, which is often used to manage variables like patient arrival times.
3 Examples
These examples will use the 2015 National Ambulatory Medical Care Survey (NAMCS) dataset from the CDC. This is a national survey designed to provide data about medical care in the United States. The specific file used, (namcs2015-spss.sav) is an SPSS data set that includes thousands of rows representing individual patient visits. Each row contains clinical information such as age, the month, and the day of the visit.
In the CDC’s NAMCS survey, date information is often provided in separate numeric columns, such as “VMONTH” for month and “VDAYR” for day. We can use these functions to synthesize this data into usable objects.
# Load the NAMCS SPSS datasetnamcs <-read_sav(here::here("function_week", "data", "namcs2015-spss.sav"))# Data before coercion namcs %>%select(VMONTH, VDAYR) %>%head(3)
# Creating a subset of the NAMCS 2015 datanamcs_sample <-tibble(VMONTH =c(10, 12, 4), # Numeric October, December, AprilVDAYR =c(2, 2, 5), # Numeric days of the week/monthAGE =c(65, 45, 75))# Sample types before coercion glimpse(namcs_sample)
# Example 1: Using as_date() to create a visit date# Combine numeric components into a string and coerce to a Date object(namcs_dates <- namcs_sample %>%mutate(date_string =paste("2015", VMONTH, VDAYR, sep ="-")) %>%mutate(Visit_Date =as_date(date_string)))
# Check: Confirm the class is now "Date"class(namcs_dates$Visit_Date)
[1] "Date"
# Example 2: Using as_datetime() and as_hms() for timestamps# Imagine we have raw character strings for patient arrival times(namcs_times <- namcs_dates %>%mutate(arrival_raw =c("08:30:00", "14:15:22", "19:05:10")) %>%# Coerce to a full Date-Time objectmutate(Arrival_DT =as_datetime(paste(date_string, arrival_raw))) %>%# Coerce just the Time using as_hms()mutate(Arrival_Time =as_hms(arrival_raw)))
Yes, these functions are helpful for health data science. In clinical settings like NAMCS, data is often collected longitudinally. Without these functions, R would treat visit dates as simple text. This makes it impossible to sort them chronologically or calculate metrics such as “days since last follow-up” or “patient age at time of visit”. These tools take messy survey characters and turn them into mathematically active time objects, which automates the data wrangling process.