data.frame() error

Hello,

I'm trying to preprocess some pupil data of mine, with the following script (sorry to post a block, but it is unclear what part of the script the error is specifically referring to):

#packages
library(tidyr)

#read ascii data
options(stringsAsFactors=FALSE)

partframe <- data.frame()

fpath <- "~/ambigPupil/Data/raw/1001_159.asc"
dat <- read.csv(fpath, header=FALSE, sep="\t", col.names=paste0("V", seq_len(9)))

#column labels - a little unwieldy because they're used for multiple types of data (i.e. saccades, fixations)
colnames(dat) <- c("Log", "Time", "Total", "xPos", "yPos", "pupil", "endy", "saccamp", "velo")

#identify trial, saccade, fixation lines in data
trialstarts <- grep("Trial image", dat$Time)
trialends <- grep("Fixation Cross Appears", dat$Time)
trial_frame <- data.frame(trialstarts, trialends)

#instead of creating two different sets of dataframes for both eyes, with monocular data you only need to gather fixations/saccades/blinks for the single eye

fixstartsL <- grep("SFIX L", dat$Log)
fixendsL <- grep("EFIX L", dat$Log)
saccstartsL <- grep("SSACC L", dat$Log)
saccendsL <- grep("ESACC L", dat$Log)
blinkstartsL <- grep("SBLINK L", dat$Log)
blinkendsL <- grep("EBLINK L", dat$Log)
fixframeL <- data.frame()
saccframeL <- data.frame()
trialframeL <- data.frame()

#fixation frame
for (trialN in 1:90){
  
  #trial, fixation, blink, saccade start/end times
  trial_start <- trialstarts[trialN]
  trial_start_time <- regmatches(dat[trial_start,2], regexpr("[0-9]+", dat[trial_start,2]))
  trial_end <- trialends[trialN]
  
  #right and left eye fixations/saccades/blinks
  fixationsL <- fixendsL[fixendsL %in% trial_start:trial_end]
  fixationsL <- dat[fixationsL,]
  #column labels: log = fixation start, time = fixation end, pupil = pupil size
  fixationsL[,1] <- regmatches(fixationsL[,1], regexpr("[0-9]+", fixationsL[,1]))
  saccadesL <- saccendsL[saccendsL %in% trial_start:trial_end]
  saccadesL <- dat[saccadesL,]
  #column labels: log = saccade start, time = saccade end, pupil = endx
  saccadesL[,1] <- regmatches(saccadesL[,1], regexpr("[0-9]+", saccadesL[,1]))
  blinksL <- blinkendsL[blinkendsL %in% trial_start:trial_end]
  blinksL <- dat[blinksL,]
  #column labels: log = blink start, time = blink end
  blinksL[,1] <- regmatches(blinksL[,1],regexpr("[0-9]+", blinksL[,1]))
  
  #check first fixation/saccade in-bounds (do they start after the trial starts?)
  if (fixationsL[1,1] < trial_start_time){
    fixationsL <- fixationsL[-1,]
  }
  if (saccadesL[1,1] < trial_start_time){
    saccadesL <- saccadesL[-1,]
  }
  
  #true blink starts and ends (saccades immediately after blinks are actually blinks)
  if (dim(blinksL)[1] > 0){
    for (blinkN in 1:nrow(blinksL)){
      if (nrow(saccadesL[saccadesL$Log < blinksL[blinkN,1] & saccadesL$Time > blinksL[blinkN,2],1:2]) > 0){
        blinksL[blinkN,1:2] <- saccadesL[saccadesL$Log < blinksL[blinkN,1] & saccadesL$Time > blinksL[blinkN,2],1:2]
      } 
    }
  }
  
  #convert to numeric
  trial_start_time <- as.numeric(trial_start_time)
  fixationsL[1:6] <- sapply(fixationsL[1:6], as.numeric)
  saccadesL[1:9] <- sapply(saccadesL[1:9], as.numeric)
  blinksL[1:3] <- sapply(blinksL[1:3], as.numeric)
  blinksL[3] <- blinksL[2] - blinksL[1]
  
  #remove blinks from saccades/fixations
  blinkremove <- function(fixtype, sacctype, blinktype){
    saccadeids <- c()
    fixationids <- c()
    for (n in 1:nrow(blinktype)){
      timediff1 <- abs(sacctype$Log - blinktype$Log[n])
      timediff2 <- abs(sacctype$Log - blinktype$Time[n])
      timediff3 <- abs(sacctype$Time - blinktype$Log[n])
      timediff4 <- abs(fixtype$Log - blinktype$Time[n])
      timediff5 <- abs(fixtype$Time - blinktype$Log[n])
      saccadeid <- c(which(timediff1 < 100), which(timediff2 < 100), which(timediff3 < 100))
      saccadeid <- unique(saccadeid)
      saccadeids <- c(saccadeids, saccadeid)
      fixationid <- c(which(timediff4 < 100), which(timediff5 < 100))
      fixationid <- unique(fixationid)
      fixationids <- c(fixationids, fixationid)
    }
    if (length(saccadeids) > 0){
      sacctype <- sacctype[-saccadeids,] 
    }
    fixtype <- fixtype[-fixationids,] 
    list(fixtype, sacctype)
  }
  #run only if there are actually blinks to remove
  if (nrow(blinksL) > 0){
    blinkremoveL <- blinkremove(fixationsL, saccadesL, blinksL)
    fixationsL <- blinkremoveL[[1]]
    saccadesL <- blinkremoveL[[2]] 
  }
  
  #remove outliers
  fixationsL <- fixationsL[fixationsL$Total < 1000,]
  fixationsL <- fixationsL[fixationsL$Total > 80,]
  
  #onset/offset times for fixations/saccades
  onsets <- function(fixtype, sacctype, blinktype){
    fixonset <- fixtype[,1] - trial_start_time
    fixoffset <- fixtype[,2] - trial_start_time
    sacconset <- sacctype[,1] - trial_start_time
    saccoffset <- sacctype[,2] - trial_start_time
    blinkonset <- blinktype[,1] - trial_start_time
    fixtype <- cbind(fixtype, fixonset, fixoffset)
    sacctype <- cbind(sacctype, sacconset, saccoffset)
    blinktype <- cbind(blinktype, blinkonset) 
    list(fixtype, sacctype, blinktype)
  }
  onsetsL <- onsets(fixationsL, saccadesL, blinksL)
  fixationsL <- onsetsL[[1]]
  saccadesL <- onsetsL[[2]]
  blinksL <- onsetsL[[3]]
  
  #remove first saccade
  #firstfixonset <- fixations[1,7]
  #saccades <- saccades[saccades$sacconset > firstfixon]
  
  #append trial # and remove unnecessary columns
  fixationsL <- fixationsL[,-c(7:9)]
  blinksL <- blinksL[,-c(4:9)]
  trial <- trialN
  if (dim(fixationsL)[1] > 0){
    fixationsL <- cbind(fixationsL, trial) 
  }
  saccadesL <- cbind(saccadesL, trial)
  
  #append to fixation/saccade frames
  fixframeL <- rbind(fixframeL, fixationsL)
  saccframeL <- rbind(saccframeL, saccadesL)
}

I'm getting the following error:

Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 0, 1

I'm having trouble figuring out which reference to data.frame() is of issue, or if there's a different issue that's causing this error. Please advise.

Thank you!

Hi, welcome!

We don't have access to your local files so we can't test your code. To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

1 Like

Hi,

Thank you, I figured out the issue - it was a small file issue that has been resolved. Thank you anyways!

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.