Introduction
This article shows you how to extract time information from timestamp variables.
Requirements
A dataset with one or more timestamp variables. The standard format for a timestamp is “Hours:Minutes:Seconds”.
Method
Converting timestamps into minutes in Q with JavaScript
- From the toolbar, select Create > Variables and Questions > Variable(s) > JavaScript Formula > Numeric.
- Copy and paste the following code into the Expression box.
var newStamp = Timestamp.split(":");
// Text would be split into “HH,MM,SS”
var hours = newStamp[0];
var minutes = newStamp[1];
var seconds = newStamp[2];
// Convert timestamp into minutes
var convertedmins = (hours * 60 ) + minutes + (seconds / 60 )
// Convert timestamp into hours
var convertedhrs = hours + (minutes / 60 )+ (seconds / 3600 ) - You can name this new variable in the Name field and give the variable a long-form label in the Label field.
- Click OK.
Converting timestamps into minutes in Q with R
- From the toolbar, select Create > Variables and Questions > Variable(s) > R Variable.
- You can name this new variable in the Variable Base Name field and give the variable a long-form label in the Question Name field.
- Enter the code into the R CODE box
x <- var ## where var is your timestamp text variable
y <- strsplit(x, “:”)
z <- do.call(rbind, y)
hours <- as.numeric(z[,1]);
minutes <- as.numeric(z[,2]);
seconds <- as.numeric(z[,3]);
# Convert timestamp into minutes
convertedmins <- (hours * 60) + minutes + (seconds / 60)
# Convert timestamp into hours
convertedhrs <- hours + (minutes / 60) + (seconds / 3600) - Click the Play button to generate a preview of the output in the window on the left. If you are happy with the results, click the Add R Variable button for Q to add variable to your project.
NEXT
How to Create a Custom R Variable