[R] Repeat
David Winsemius
dwinsemius at comcast.net
Sat Feb 25 19:14:22 CET 2017
> On Feb 25, 2017, at 8:09 AM, Ashta <sewashm at gmail.com> wrote:
>
> I have a data set and I want to repeat a column value based on other
> column value,
>
> my data look like
>
> read.table(text = "Year month flag
> 2001 1 Z
> 2001 2 -
> 2001 4 X
> 2002 1 Z
> 2002 2 -
> 2003 1 -
> 2003 2 Z
> 2004 2 Z
> 2005 3 Z
> 2005 2 -
> 2005 3 -", header = TRUE)
>
> Within year If flag = '-' then i want replace '-' by the previous
> row value of flag. In this example for yea 2001 in month 2 flag is
> '-' and I want replace it by the previous value of flag (i.e., 'Z')
> 2001 1 Z
> 2001 2 Z
> 2001 4 X
>
> If all values of flag are '-' within year then I wan to set as N
>
> The complete out put result will be
>
> year month flag
> 2001 1 Z
> 2001 2 z
> 2001 4 X
> 2002 1 Z
> 2002 2 Z
> 2003 1 Z
> 2003 2 Z
> 2004 2 Z
> 2005 3 Z
> 2005 2 N
> 2005 3 N
>
> Thank you in advance
>
Your example doesn't actually match your verbal description of the algorithm because you have not specified the rule that establishes values for instances where the first value in a year is "-".
The `na.locf` function in the 'zoo' package would be useful for the task describe in your verbal description when used in conjunction with the 'stats'-package's `ave` function.
--
David.
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list