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

From phurvitz
< Main Page‎ | Research‎ | MSB‎ | Scripts
Jump to: navigation, search
# timestamp fuzzy match
# finds the closest match for timestamp + subject.
# adds the seconds.msb field to the MyExperience table

# a function
timestamp.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)[ncol(my.experience)] <- "msb.seconds"
    
    # output
    output.file = paste(indir, "myexp_ts.csv", sep="/")
    write.table(my.experience, file=output.file, row.names=F, col.names=F, sep=",", quote=F)
    
    # function return
    return(my.experience)
}