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,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
In a next step i search for all Indices
which have the value -2
but I want the value next to it so my code is:
which(eData == -2) + 1
[1] 25 114 118 120 124 128 132 138 140 146 164 166 173 177 183 186 190 194 281 343 387 456
Looking at the actual data I want all values > 0
:
eData[which(eData == -2) + 1]
[1] 0 0 2 2 0 0 0 2 0 0 2 0 0 0 0 0 1 0 0 0 0 0
So i do:
which( eData[which(eData == -2) + 1] > 0)
3 4 8 11 17
But the final result I really want is:
result
118 120 138 164 190
Meaning the corresponding Indices for the original data. So that i can do:
eData[result]
2 2 2 2 1
like
eData[118]
2
etc.
How can I archive something like this all I tried yielded just an error message or got me just the "wrong" result 3 4 8 11 17
:
which( eData[which(eData == -2) + 1] > 0)
But I want the Original values. I have the impression that it cant be that difficult, but cant figure it out myself.
Upvotes: 0
Views: 73
Reputation: 3678
From what you are saying, you are looking for strictly positive values which are after a value equal to -2. You can do an intersect
in order to have the position of those numbers.
intersect(which(eData >0),which(eData == -2)+1)
# [1] 118 120 138 164 190
and you indeed have
eData[intersect(which(eData >0),which(eData == -2)+1)]
# [1] 2 2 2 2 1 # the values of eData(118), ... : all > 0
eData[intersect(which(eData >0),which(eData == -2)+1)-1]
# [1] -2 -2 -2 -2 -2 # the values of eData(118-1), ... : all equal to -2
or you could also use
which(eData>0 & c(NA,eData[-length(eData)])==-2)
# [1] 118 120 138 164 190
Upvotes: 2