
Hey Michael and Tyler!

Thanks for the input and the effort! The lubridate package is really awesome!

What I did now is just this here:

dat$DATE<-as.Date(paste(dat$YEAR,dat$MONTH,dat$DAY,sep='-'),format='%Y-%m-%d')

require(lubridate)
dat$JULIAN<-yday(dat$DATE)


That's it. Nice and easy :)

Thanks!
Stefan

-----Original Message-----
From: Tyler Rinker [mailto:tyler_rinker@hotmail.com]
Sent: Mon 4/30/2012 6:40 PM
To: Schreiber, Stefan; r-help@r-project.org
Subject: RE: [R] for loop problem
 


I don't really work with dates but thought I'd pass a solution on.  I think that there some great packages for handling dates though (lubridate) and you may want to convert your data to a true date instead of separate columns.

# FUNCTION TO INDEX DATES


date.int <- function(month, year, day){


    yearLength <- function(year) year %% 4 == 0


    key1 <- data.frame(m=1:12, nm=c(31, 28, 31, 30, 31, 30, 31, 30, 31, 31, 30, 31))


    key1$sum <- c(0, cumsum(key1$nm)[-12])


    key2 <- data.frame(m=1:12, nm=c(31, 29, 31, 30, 31, 30, 31, 30, 31, 31, 30, 31))


    key2$sum <- c(0, cumsum(key2$nm)[-12])


    ifelse(yearLength(year), key1[month-1, 3] + day, key2[month-1, 3] + day)


}


#TRY IT OUT ON A MADE UP DATA SET


M <- sort(sample(seq(as.Date("2000/1/1"), by="day", length.out=1000), 10))


N <- do.call(rbind, strsplit(as.character(M), "\\-"))


N <- data.frame(apply(N, 2, as.numeric))


colnames(N) <- c('year', 'month', 'day')


with(N, date.int(month=month, year=year, day=day))


----------------------------------------
> Date: Mon, 30 Apr 2012 17:57:02 -0600
> From: Stefan.Schreiber@ales.ualberta.ca
> To: r-help@r-project.org
> Subject: [R] for loop problem
>
> 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.
 		 	   		  



	[[alternative HTML version deleted]]

