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 >
> On Feb 25, 2017, at 10:45 AM, Ashta <sewashm at gmail.com> wrote: > > 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 - >I was a bit surprised by the results of htis since I had assumed than an initial NA in a group would remain so, but apparently not: dat$new <- with(dat, ave(flag, Year, FUN=function(s){ s[s=="-"] <- NA; zoo::na.locf(s) }) )> datYear month flag new 1 2001 1 Z Z 2 2001 2 - Z 3 2001 4 X X 4 2002 1 Z Z 5 2002 2 - Z 6 2003 1 - Z 7 2003 2 Z Z 8 2004 2 Z Z 9 2005 3 Z Z 10 2005 2 - Z 11 2005 3 - Z David.> > > 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 >>David Winsemius Alameda, CA, USA
Thank you so much David! But if all element of a group has '-' did not work. In this case year 2006 an example If all values of flag are '-' within year then I wan to set as N dat=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 - 2006 1 - 2006 2 - ", header = TRUE) dat$new <- with(dat, ave(flag, Year, FUN=function(s){ s[s=="-"] <- NA; zoo::na.locf(s) }) ) Error in `[<-.factor`(`*tmp*`, i, value = integer(0)) : replacement has length zero On Sat, Feb 25, 2017 at 5:43 PM, David Winsemius <dwinsemius at comcast.net> wrote:> >> On Feb 25, 2017, at 10:45 AM, Ashta <sewashm at gmail.com> wrote: >> >> 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 - >> > > I was a bit surprised by the results of htis since I had assumed than an initial NA in a group would remain so, but apparently not: > > dat$new <- with(dat, ave(flag, Year, FUN=function(s){ s[s=="-"] <- NA; zoo::na.locf(s) }) ) > >> dat > Year month flag new > 1 2001 1 Z Z > 2 2001 2 - Z > 3 2001 4 X X > 4 2002 1 Z Z > 5 2002 2 - Z > 6 2003 1 - Z > 7 2003 2 Z Z > 8 2004 2 Z Z > 9 2005 3 Z Z > 10 2005 2 - Z > 11 2005 3 - Z > > David. > >> >> >> 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 >>> > > David Winsemius > Alameda, CA, USA >