Main Page/Research/MSB/Scripts/conflate timestamp.R

From phurvitz
< Main Page‎ | Research‎ | MSB‎ | Scripts
Revision as of 21:23, 11 October 2007 by Phil Hurvitz (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
  1. timestamp fuzzy match
  2. finds the closest match for timestamp + subject.
  3. adds the seconds.msb field to the MyExperience table
  1. a function

timstamp.match <- function(indir, minutes.offset=10) {

   # offset for matching tolerance
   seconds.offset <- minutes.offset * 60
   
   # read in the data sets
   # phone log (from processing of the mailman logs in read.msb_files.3.R
   phone.log.file <- paste(indir, "phone_log.csv", sep="")
   phone.log <- read.csv(phone.log.file, stringsAsFactors=F)
   my.experience.file <- paste(indir, "myexper.csv", sep="")
   my.experience <- read.csv(my.experience.file, stringsAsFactors=F)
   
   # process each record in the phone log file
   for (i in 1:nrow(phone.log)) {
       # get the timestamp from the current record
       timestamp <- as.POSIXct(phone.log[i,6])
       # get the MSB seconds value from the current record
       msb.seconds <- as.numeric(phone.log[i,3])
       # get the subject number from the current record
       subject.num <- phone.log$subject.num[i]
       # create a T/F vector indicating which record in the MyExperience file
           # is within 10 minutes of the current phonelog timestamp AND
           # has the same subject ID
       time.match <- 
           ((my.experience$date.phone > (timestamp - seconds.offset)) &
           (my.experience$date.phone < (timestamp + seconds.offset)) &
           (my.experience$sid == subject.num))
       # set the MSB seconds for the record that matches from the T/F matrix
       my.experience[time.match,12] <- msb.seconds
   }
   
   # fix the new column name
   colnames(my.experience) <- c(colnames(my.experience)[1:ncol(my.experience)-1], "msb.seconds")
   return(my.experience)

}