mercredi 1 juillet 2015

R - For loop with If statement producing non-sequential cycle

I have two measurements I'm trying to collapse into a single column of data. All subjects gave the first kind of measurement, some gave the second. If the second kind is available, it should be imported into the new column. If the second measurement isn't available for that subject, at least the first one is, so that is the measurement that should be imported for that subject. For instance:

subject measureA measureB collapsed
1          X        A        A
2          Y        B        B
3          Z        -        Z

Again, measureB has preference, but if no measureB is available for a given subject, the collapsed column should use measureA.

Here is the code I've used to try to get this to work:

i = 0
newDV = 0
newID = 0

data$subject = 1:135 in frame with measureA
conditional$subject = 1,2,4,5,9,11, etc in frame with measureB

for(i in data$subject) {
    if(data$subject[i] %in% conditional$subject) {newDV[i] <- 
conditional$measureB[conditional$subject[i]]
            newID[i] = conditional$subject[i]
        }else {newDV[i] <- data$measureA[data$subject[i]]
            newID[i] = data$subject[i]}}

I then merged the newID and newDV columns to check that the loop worked correctly. It did not. Here is a snippet from the merged data frame:

newID     newDV
1       1 1.0000000
2       2 0.0000000
3       3 0.7500000
4       5 1.0000000
5       9 0.1666667
6       6 0.3750000
7       7 0.0000000
8       8 0.2500000
9      14 0.2500000

Clearly it is not doing what I hoped; it is skipping several subject numbers. The dataframe gets even stranger further down:

40     40 0.1875000
41     41 0.0625000
42     42 0.2500000
43     75        NA
44     44 0.2500000
45     NA        NA
46     80        NA

It is not progressing sequentially, and leaving NAs (none of this data should be missing).

Is it clear to anyone why the For loop and the code within it are not performing the function I'm trying to execute?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire