mardi 21 février 2017

IFELSE-Statement throughout different rows

i'm relatively new to R and i've got a problem with applying an ifelse-statement throughout multiple rows. my data looks like this. the first column indicates the number of the variable, A=1 is the first time you see a task, A=3 is the second time you see the same task. B = 1 means correct, C = 1 means false (and vice versa with the 0s).

A B C
1 1 1 0
2 1 1 0
3 1 0 1
4 1 0 1
5 1 1 0
6 1 1 0
3 3 1 0 4 3 0 1

now what I want to do, is to add a colum D with a value 1, everytime B=1 and everytime when B=0 in the first trial (A=1) but B=1 in the second trial. the data frame looks like this:

A B C D
1 1 1 0 1 2 1 1 0 1 3 1 0 1 1 4 1 0 1 0 5 1 1 0 1 6 1 1 0 1 3 3 1 0 1 4 3 0 1 1

I thought about something like D <- ifelse(B == 1 & A == 3, 1, NA)

or

D <- ifelse(B == 1, 1, NA)

or

D <- ifelse(C == 1, 0, NA)

but obviously none of this will work, because I didn't connect the different rows. i try to figure out some code that tells:

if i have in any row B=1 OR in a row with A=3 for B=1 i want 1 in D for both corresponding trials.

thx for your help!

Aucun commentaire:

Enregistrer un commentaire