vendredi 2 décembre 2016

if statement argument length 0 in R

Hopefully a simple question:

I am getting an "argument is of length zero" for the if statement line:

 for (i in 1:(length(MixedDF))) {
    if (MixedDF[i,1] - MixedDF[i-1,1] == 1) {
    SwitchInd[i] = MixedDF$trial[i]
}

}

Where MixedDF is a large matrix and SwitchInd is a matrix of zeroes that is supposed to get filled in with the indices identified in the if statement. MixedDF$trial or MixedDF[i,1] is the first column in the matrix. This column contains integers starting at 51 and going to 74, where there are many rows with the same value. So for example MixedDF$trial <- c(51, 51, 51, 51, 51, 52, 52, 52, 52, 52, 52, 52, 52, 52, 53, 53.....). I want to identify the indices where the trial changes, so 51 to 52, 52 to 53 and so on. More generally I want to understand why the if statement isn't working, it seems straightforward.

Aucun commentaire:

Enregistrer un commentaire