Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: The replace function

From   n j cox <[email protected]>
To   [email protected]
Subject   st: RE: The replace function
Date   Mon, 11 Jul 2005 16:27:41 +0100

Note that Leny Mathew's statement about the -replace-
command (N.B. not function) is false. There is no
reason why -if- and/or -in- restrictions should not
be used, as the on-line help makes plain, to -replace-
individual values.

In any case, a moderately bizarre alternative solution is

replace foo = foo[cond(mod(_n,4),_n + 4 - mod(_n,4),_n)]

I doubt that this generalises easily or usefully, but it may amuse.

In practice, a solution like Tom's is usually preferable.

Steichen, Thomas J.

> Something like the following should work:

>  . gen tempgrp = int(_n/4 +.75)
>  . bysort tempgrp: replace var = var[_N]
>  . drop tempgrp

Leny Mathew

>     I have a vector variable in which I want to replace the
> first three
> values by the fourth value; the fifth, sixth, seventh by the eight
> value; and so on till the end- the 960th value. This could be
> done with
> a loop, but the replace function in stata does not allow me
> to replace
> a specific observation in the vector. It allows me to replace the
> vector as a whole. Does any body know of a way in which the function
> can be made to replace specific values? Is there any other
> way of doing this?

*   For searches and help try:

© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index