[prev in list] [next in list] [prev in thread] [next in thread] 

List:       r-help
Subject:    [R] for loop problem
From:       "Schreiber, Stefan" <Stefan.Schreiber () ales ! ualberta ! ca>
Date:       2012-04-30 23:57:02
Message-ID: 70F02259E17B6242B15D81E58EB7EB11087B5BBE () afhe-ex ! afhe ! ualberta ! ca
[Download RAW message or body]

Hi all,

I was wondering if you can help me with the following situation:

I have a data frame that includes weather station data for 30 years in
the form:

YEAR, MONTH, DAY, TEMP
1970, 01, 01, -15
...
1999, 12, 31, -21

I would like to add another variable "JULIAN" that assigns the integers
1 to 365 (and 1 to 366 for leap years) for each day of a year over
multiple years.

Here is what I came up with:

counter<-1

for(i in 1:12){
for (j in 1:31){

df$JULIAN[df$MONTH==i & stn$DAY==j]<- counter
counter<-counter+1
}
}

R does it exactly what I told it to but I am not satisfied with it since
it doesn't stop assigning the integers when months have 28, 29 or 30
days. For instance on Feb-28 JULIAN is 59 and on March-1 it's 64, as
opposed to be 60.

I am assuming it must be an ifelse statement, and I was messing around
with it already but without success.

I guess I am missing some vocabulary here and hope someone can give me
some pointers.

Thanks,
Stefan  

	[[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
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.
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic