If I understand correctly, you can't recode the same variable, but you
can create a new variable with the same categories and a different mapping.
Thanks for the tips
Paolo
Il 01/02/2023 16:12, Cottrell, Allin ha scritto:
On Wed, Feb 1, 2023 at 9:03 AM Cottrell, Allin
<cottrell(a)wfu.edu> wrote:
> On Wed, Feb 1, 2023 at 5:02 AM Paolo Chirico <paolo.chirico(a)uniupo.it> wrote:
>> Dears,
>> If I have a string variable with the following mapping:
>> 1 = 'middle'
>> 2 = 'low'
>> 3 = 'high'
>> there is a command to give the series a new, more logical mapping? Like as:
>> 1 = 'low'
>> 2 = 'middle'
>> 3 = 'high'
>> I've simplified the example, but the concept is: changing the numerical
>> mapping of the entire series
[...]
While my previous answer works for the simplified case, it may be
worth giving a more general, automated solution. Here's a function
that does the job:
<hansl>
function void recode (series *y, series x, strings S)
y = 0
matrix m = {y}
strings S0 = x
loop i=1..nelem(S)
matrix sel = instrings(S0, S[i])
m[sel] = i
endloop
y = m
stringify(y, S)
end function
</hansl>
The idea is that you pass (1) a target series in pointer form, (2) the
original string-valued series and (3) an array holding the string
values of the original series, re-ordered as per the desired numerical
coding. So in relation to my previous example you'd do:
<caller>
open strs.csv
series y
recode(&y, x, defarray("low", "middle", "high"))
</caller>
Allin
_______________________________________________
Gretl-users mailing list -- gretl-users(a)gretlml.univpm.it
To unsubscribe send an email to gretl-users-leave(a)gretlml.univpm.it
Website:
https://gretlml.univpm.it/postorius/lists/gretl-users.gretlml.univpm.it/
--
Paolo Chirico
Università del Piemonte Orientale
Dip. di Giurisprudenza e Scienze Politiche,
Economiche e Sociali (DIGSPES)
Alessandria, Italia