S. Proa S. Proa - 29 days ago 9
R Question

Working with a matrix in R by rows and columns and deleting rows iteratively

I want to compute the time interval that has been needed for pressing the different keys of the keyboard by writing a message.

In order to get the data I use a program that produces a .csv after writing a text. This .csv has three columns: the first one with the key pressed and released, the second column says if the key has been pressed (0) or released (1), and the last column registers the time for each event.

Then the idea is to compute the time interval that has been needed for each key, since it is pressed until it is released.

In the following extra simple example we can see that the key

16777248
has been pressed at time
5.0067901611328125e-06
and released at time
0.21875882148742676
, therefore, the time interval for this key is
0.21875882148742676-5.0067901611328125e-06
. The time interval for the key
72
should be
0.1861410140991211-0.08675289154052734
.

16777248 0 5.0067901611328125e-06

72 0 0.08675289154052734

72 1 0.1861410140991211

16777248 1 0.21875882148742676


At the moment I have written a code in R that, first of all, reads the table in .csv. Then it searchs the first 1 in the second column and takes the corresponding key name. Next, it searchs for the previous key with a 0. It computes the time interval, saves this value in a vector and then deletes this two rows from the matrix. Following, it should repeat this until the are no more rows.

data.csv <- read.table("example.csv",header=F, sep=",", dec=".")
myTable<- data.csv
keySearched=0
timeInterval=c( rep( 0,length(myTable[,1]) ) )
L=(length(myTable[,1]))

for( i in 1:L ){
if( myTable[i,2]==1 ){
keySearched <- myTable[i,1]
for( j in 1:(i-1) ){
if( myTable[j,1]==keySearched ){
timeInterval[j] <- (myTable[i,3]-myTable[j,3])
myTable <- myTable[ -c(j,i), ]
}
}
}

}


The problem is that sometimes the value myTable[x,y] is NA because the corresponding row has been deleted. In each iteration two rows are deleted (the one with the pressed key and the corresponding released key).

At this point I get the following error:

Error in if (myTable[j, 1] == keySearched) { :
missing value where TRUE/FALSE needed


How could I solve this problem?

Answer

You could try doing it like this:

key = c(3,6,3,8,8,3,6,3)
pressed = c(0,0,1,0,1,0,1,1)
time = c(12,14,16,17,19,22,34,35)
a = data.frame(key,time,pressed)

>a
  key time pressed
1   3   12       0
2   6   14       0
3   3   16       1
4   8   17       0
5   8   19       1
6   3   22       0
7   6   34       1
8   3   35       1

First order your data frame (or matrix if you prefer) by the key number and then the time. This should group pressed and released keys together. Then you calculate the time difference between the same keys using diff. And finally, set to NA those diffs that don't make sense.

a = a[order(a$key,a$time),]
a$lapse = c(0,diff(a$time))
a$lapse[seq(1,nrow(a),2)] = NA

>a
  key time pressed lapse
1   3   12       0    NA
3   3   16       1     4
6   3   22       0    NA
8   3   35       1    13
2   6   14       0    NA
7   6   34       1    20
4   8   17       0    NA
5   8   19       1     2