vendredi 11 juin 2021

If two observations have the same value in consecutive positions, select depending on the values of a different column

I am working with R studio.

I have a data set that looks like this...

Condition  TargetWord             WordProduced        WPcondition    realValue                                                               1          Target1                  table                 P              .025
1          Target1                  word                  P              .009
1          Target1                  chair                 P              .005
1          Target1                  pole                  Q              .015
1          Target1                  skate                 Q              .023
1          Target2                  car                   Q              .014
1          Target2                  house                 P              .014
1          Target2                  shoes                 P              .019
1          Target2                  girl                  Q              .011
1          Target2                  life                  Q              .020
1          Target3                  computer              Q              .007
1          Target3                  ball                  Q              .007
1          Target3                  court                 F              .009
1          Target3                  plane                 C              .035
1          Target3                  sky                   D              .008
2          Target4                  tree                  P              .051
2          Target4                  five                  P              .051
2          Target4                  help                  Q              .003
2          Target4                  shave                 Q              .006
2          Target4                  love                  P              .028
2          Target5                  three                 P              .056
2          Target5                  file                  Q              .056
2          Target5                  hemp                  P              .003
2          Target5                  share                 P              .006
2          Target5                  long                  Q              .028
2          Target6                  ten                   Q              .058
2          Target6                  friend                P              .051
2          Target6                  hail                  Q              .003
2          Target6                  shine                 P              .006
2          Target6                  loner                 P              .028

So, every target is repeated five times and I need to filter for the first. The problem that I have is that if the reaValue is the same (.014 & .014) in the first two positions I need the one have a value of P under the WPcondition.

This is, before filtering for the first position, if I have a tie of the realValue within the first two positions, then I need to look at the column at left (WPcondition) to see if one of them is a P. If one of them it is a P, then I need to have that one in the first position.

Let's say...

1position P .05 2position P .05 (stay with the one that it is in the first position)

1position Q .05 2position P .05 (Use the one that it is in the second position because it has a P)

1position Q .05 2position Q .05 (stay with the one that it is in the first position)

1position P .05 2position Q .05 (stay with the one that it is in the first position)

1position P .06 2position Q .05 (stay with the one that it is in the first position because the realValue is higher)

1position Q .06 2position P .05 (stay with the one that it is in the first position because the realValue is higher)

So, I need to stay with the one that has the higher value, but if the value is the same we need to considerate the P&Q value and if There is a P, the choose that one.

Any help would be great.

Thanks.

Aucun commentaire:

Enregistrer un commentaire