jeudi 11 février 2021

Replacing cells in a data frame

Let's say I have such a data frame with columns X, Y, Z, T and over 100 rows:

order X Y Z T
 i    a k b n
 j    c a b n

As you see, if i-th X and j-th Y have the same value (i.e a), then i-th Z and j-th Z have the same value (b) and i-th T and j-th T have the same value (n)

What I want to do is that if i-th X and j-th Y have the same value (i.e a), then i-th Z = b and j-th Z = n and i-th T = n, j-th T = b

order  X Y Z T
   i   a k b n
   j   c a n b

I have tried doing this in R by using if else and for loop, but I couldnt.

Can anyone help me do that in R?

Aucun commentaire:

Enregistrer un commentaire