Hi lily, It might be easier doing it like this: DF<-data.frame(year=rep(1972:1985,each=12), month=rep(1:12,14),day=rep(1,168)) DF$time<-paste(DF$year,DF$month,DF$day,sep="-") DF$flow<-runif(168,4,7) DF$wyear<-DF$year + (DF$month > 9)
Jim On Wed, Jul 5, 2017 at 4:31 AM, lily li <chocol...@gmail.com> wrote: > Hi R users, > I have a question about adding a column for water year. The dataframe has > the structure below. But the wyear column just shows one year. Could anyone > help me with this problem? Thanks. > > DF > year month day time flow > 1972 1 1 1972-01-01 5 > 1972 1 2 1972-01-02 5.5 > 1972 1 3 1972-01-03 6 > ... > 1985 12 31 1985-12-31 6 > > > for(i in 1972:1985){ > if(DF$year==i & DF$month %in% 1:9){ > DF$wyear <- i { > }else{ > DF$wyear < i-1 > } > } > } > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. ______________________________________________ R-help@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.