[R] Repeat
Ashta
sewashm at gmail.com
Sat Feb 25 19:45:58 CET 2017
Thank you David.
is it not possible to sort it by year and flag so that we can make '-'
in the second row ? like this for that particular year.
2003 2 Z
2003 1 -
On Sat, Feb 25, 2017 at 12:14 PM, David Winsemius
<dwinsemius at comcast.net> wrote:
>
>> 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