Skip to content

Calculate elapsed time in minutes and seconds

September 20, 2013

A couple days ago, I received a request from a friend to help him calculate the time taken for respondents to fill out a survey. Since many respondents changed their minds (i.e., gave up) in only a few seconds, so my friend also asked me if I can calculate and show this duration in minutes and seconds.

The original survey data is in SPSS sav format, since I don’t own a SPSS copy and my SPSS skills are getting raw, so I did it in R. Below is what I did and the biggest challenge turned out to be converting Julian data “%m/%d/%y” to the standard “m-d-y H:M:S” format.

###############################################################################
library(foreign) ###required for converting SPSS files###
library(memisc) ###required to read SPSS table###
library(lubridate) ###required for duration calculation###
library(epitools) ###required for converting Julian date format###
setwd(“C://”)
data <- as.data.set(spss.system.file('file-directory'))

###check data###
names(data)

###convert Julian dates to "%m-%d-%y %H:%M:%S" format using as.POSIXct function###
SD <- as.POSIXct(as.character(data$StartDate), format = "%m/%d/%Y %H:%M:%S")
ED <- as.POSIXct(as.character(data$EndDate), format = "%m/%d/%Y %H:%M:%S")

###then, calculate duration in seconds using the built-in difftime function###
tdinsecs <- difftime(ED, SD, units='secs')
tdinsecs

###get largest integers not greater than the corresponding tdinsecs using floor argument###
minutes <- floor((tdinsecs/60))

###subtract from tdinsecs to calculate the remaining seconds###
seconds <- tdinsecs – (minutes*60)

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: