Im am trying to figure out how to subset my dataset according to the repeated value of the variable s
, taking also into account the id
associated to the row.
Suppose my dataset is:
dat <- read.table(text = "
id s
1 2
1 2
1 1
1 3
1 3
1 3
2 3
2 3
3 2
3 2",
header=TRUE)
What I would like to do is, for each id
, to keep only the first row for which s = 3
. The result with dat
would be:
id s
1 2
1 2
1 1
1 3
2 3
3 2
3 2
I have tried to use both duplicated()
and which()
for using subset()
in a second moment, but I am not going anywhere. The main problem is that it is not sufficient to isolate the first row of the s = 3
"blocks", because in some cases (as here between id = 1
and id = 2
) the 3's overlap between one id and another.. Which strategy would you adopt?