Reputation: 710
I hope someone can help me in cleaning my data using R.
Sample data:
seqno rectyp ps04
1 1
1 1
1 89
1 89
2 1 0
2 1 1
2 1
2 90
2 89
3 1
3 1
3 1
3 90
3 90
3 89 1
3 89 5
3 89 6
3 89
What I would like to do is look for values ps04==1
and rectype==1
for a certain seqno
value. Once found, it will fill out the whole rows of that seqno
with value 1
. Expected output would be:
seqno rectyp ps04
1 1
1 1
1 89
1 89
2 1 1
2 1 1
2 1 1
2 90 1
2 89 1
3 1
3 1
3 1
3 90
3 90
3 89 1
3 89 5
3 89 6
3 89
Thanks for your help!
Upvotes: 0
Views: 821
Reputation: 8488
First see what rows meet t$ps04==1 & t$rectyp==1
. Get the seqno
for those and then make i
equal TRUE
for all the rows that meet the condition.
> i <- t$seqno %in% t$seqno[t$ps04==1 & t$rectyp==1]
> i
[1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> t$ps04[i] <- 1 # set values to 1
> t
seqno rectyp ps04
1 1 1 NA
2 1 1 NA
3 1 89 NA
4 1 89 NA
5 2 1 1
6 2 1 1
7 2 1 1
8 2 90 1
9 2 89 1
10 3 1 NA
11 3 1 NA
12 3 1 NA
13 3 90 NA
14 3 90 NA
15 3 89 1
16 3 89 5
17 3 89 6
18 3 89 NA
Upvotes: 1