Folks,
from time to time I need to check if a certain object belongs or not to a
set of integers. For example, suppose you have a panel and you want to
select only certain units, whose id you list in a vector.
This can of course be generalised, so I wrote a little hansl function (I
called it any() because of my lack of imagination: alternative proposals
are very welcome):
<hansl>
set verbose off
set seed 999
function numeric any(numeric X, const matrix A)
# checks if X belongs to the set A
if typeof(X) == 1
# scalar
scalar ret = max(A .= X)
elif typeof(X) == 2
# series
series valid = ok(X)
series ret = NA
smpl valid --dummy
matrix tmp = { X } .= vec(A)'
ret = sumr(tmp) .> 0
smpl full
elif typeof(X) == 3
# matrix
scalar r = rows(X)
scalar c = cols(X)
matrix ret = maxr(vec(X) .= vec(A)')
ret = mshape(ret, r, c)
endif
return ret
end function
###
### usage example
###
nulldata 20
# scalar
foo = {1, 3, 11}
loop i = 1 .. 6
eval any(i, foo)
endloop
# matrix
matrix Z = mrandgen(i, 1, 6, 6, 3)
print Z
eval any(Z, foo)
# series
series x = randgen(i, 1, 6)
x[5] = NA
series y = any(x, foo)
print x y --byobs
</hansl>
If this is useful, we could either (a) add this to the extra package or
(b) make this a native libgretl function. Comments?
-------------------------------------------------------
Riccardo (Jack) Lucchetti
Dipartimento di Scienze Economiche e Sociali (DiSES)
Università Politecnica delle Marche
(formerly known as Università di Ancona)
r.lucchetti(a)univpm.it
http://www2.econ.univpm.it/servizi/hpp/lucchetti
-------------------------------------------------------