0

I want to get rid of repeated measures in my data file. The idea is that people pressed the space bar and I want to eliminate rows when participants pressed the key more frequently than once in 3 seconds. I created this loop, but I see that it doesn't remove all the cases and it throws an error because the number of rows changes all the time.

Do you have an idea, of how to change this loop to make it serve its purpose correctly?

df_window_rem <- df_window
for (i in 2:nrow(df_window_rem)) {
  p <- df_window_rem[[i,3]] ##cell with time people presses
  q <- df_window_rem[[i-1,3]] ##cell with the previous click
  x <- df_window_rem[[i,4]] ##score 0/1 for this cell
  y <- df_window_rem[[i-1,4]] ##score 0/1 for previous cell
  if((p-q)<3){
    if (x == 1 & y == 0) {
      df_window_rem <- df_window_rem[-(i-1)]
    } else if (x == 0 & y == 1){
      df_window_rem <- df_window_rem[-i]
    } else if (x == 1 & y == 1) {
       df_window_rem<- df_window_rem[-i]
    }
  }
}
holholhol
  • 11
  • 1
  • Please make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) or [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) with a sample input and your expected output. [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) Based on your input we can understand your problem and think about a possible solution and verify it compared to your expected output. – Martin Gal Nov 12 '22 at 16:25

2 Answers2

0

I am not sure what your variables are measuring. I assume that you have a variable "first press of the space bar" and a variable "second press of the space bar". If this is the case then you can use the subset() function with a condition based on this 2 variables.

data <- data.frame(first_press = c(1,1,1,1,1),                 
               second_press = c(2,3,4,5,6))
data_clean <- subset(data, second_press-first_press >2)
head(data_clean)

This code checks for every row of the dataframe if the second_press occured more than 2 seconds after the first press. If that is the case, the row is not included in the subset.

Scasto
  • 34
  • 3
  • Unfortunately, that is not what I meant. In my case, one variable is timepoints when a person presses the spacebar, and the other one indicates if the answer was right or not. So variable in column 3 is a series of timepoints when a person pressed the spacebar. – holholhol Nov 09 '22 at 14:15
0

An approach is don't try to delete the rows within the loop since that messes up the indexing. Instead collect the index of the rows you want to delete and then after the loop completes, just use the negative of this vector of indices to delete the rows.