[R] about adding a column for water year
Bert Gunter
bgunter.4567 at gmail.com
Tue Jul 4 21:20:24 CEST 2017
Well, let's see:
1) You do not appear to understand basic flow control statements in R.
Note that (from ?if):
if(cond) expr
if(cond) cons.expr else alt.expr
where
"cond A length-one logical vector that is not NA."
Your cond is a vector of length nrow(DF), so you don't want if, you
want ifelse().
Did you fail to show us your warning messages??
2. Revising your code and eliminating the extraneous brackets, one gets:
for(i in 1972:1985){
ifelse(DF$year==i & DF$month %in% 1:9, DF$wyear <- i,
DF$wyear < i-1)
}
But that doesn't work either, giving only 1985. Why? -- because the
assignment statement DF$wyear <- i assigns the single value i to the
whole column. So the whole column gets the last value of 1985, which
is presumably what you saw but neglected to tell us.
3. That can be fixed by ditching the loop and ising ifelse() properly:
DF$wyear <-ifelse(DF$month %in% 1:9,DF$year ,DF$year-1)
or even more succinctly, albeit with a trick (automatic coercion of
logical to numeric)
DF$wyear <- DF$year - (DF$month %in% 10:12)
This is an example of vectorization, a powerful feature of R that
would be worthwhile for you to learn. Your initial use of a C like
for() loop should be avoided when possible, as it could be here.
If I have made an error in understanding what you are doing, please do
let us all know. I get it wrong from time to time.
Cheers,
Bert
Bert Gunter
"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Tue, Jul 4, 2017 at 11:31 AM, lily li <chocold12 at 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 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.
More information about the R-help
mailing list