[R] Repeat

Ashta sewashm at gmail.com
Sat Feb 25 17:09:38 CET 2017


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



More information about the R-help mailing list