Using R to Read Eye-tracking Files (.txt, .tsv, ect.) and Organize Data

April 25, 2017
By

[This article was first published on Hui Tang's R Site, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If a researcher is interested in certain time periods of an eye-tracking recording, such as problem-solving phases in education studies, he/she can replay the recording, generate segments or scenes, and then export the relevant data. However, this process is time-consuming. Using R may provide a way to quickly obtain and organize such data (when the definitions of time periods are clear). Here I use the text files exported by Tobii Studio as an example.
Suppose there are 15 .tsv files (which can be considered as .txt files) in a local folder “D:\tsv-aoi”. Each file contains the recording of a participant in an eye-tracking experiment.

files15

The first step is to get all the 15 file names:

setwd(“D:/txt-aoi/”)
filenames <- list.files(full.names=TRUE)

Each file looks like:

file-content

If we plan to obtain information between the first AOI11_1 (Timesamp=306913 in the above file) and the first AOI11_1 coming back (Timesamp=319356 in the above file), the next step is to read all 15 files into (a list of) data frames (notice the first 11 lines in each file are not needed), get the “Timestamp” and “AoiNames” columns for AOI11’s only.

aoiFiles <- lapply(filenames, function(x){
                      read.table(x, header=TRUE,  sep=”\t”, skip=11)
})
aoiFiles_q11 <- lapply(aoiFiles, function(x){
                               x[grep(“tor11.html”, x$StimuliName), c(1,4)]
})
aoiFiles_q11

The result (showing from one of the 15 files):

column1-4

We may remove consecutive duplicate AOIs from each data frame:

RmRepeat <- function(df, n){
   X <- rle(as.character(df[,n]))
   Y <- cumsum(c(1, X$lengths[-length(X$lengths)]))
   return(df[Y,])
}
aoiFiles_q11rd <- lapply(aoiFiles, function(x) RmRepeat(x, 2))
aoiFiles_q11rd

 

The final result (one of the 15) is shown below. From there we can analyze eye-tracking measures such as fixation duration and scanpath between the 2 red time stamps.

result

 

 

 

 

 

 

To leave a comment for the author, please follow the link and comment on their blog: Hui Tang's R Site.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)