[R] how di I write a for loop in which i is in POSIX?

peregrine halconperegrino at gmx.de
Thu May 8 23:12:12 CEST 2014


Hallo,

I have a table in which I would like to insert the min and max values of
another colum (date and time in as.POSIXct Format).

This is my row table, where "su" and "sa" are still the same as "Vollzeit":

head(treat)
             Vollzeit      Datum   Zugnacht                  su                 
sa
2 2013-09-09 20:15:00 2013-09-09 2013-09-09 2013-09-09 20:15:00 2013-09-09
20:15:00
3 2013-09-09 20:30:00 2013-09-09 2013-09-09 2013-09-09 20:30:00 2013-09-09
20:30:00
4 2013-09-09 20:45:00 2013-09-09 2013-09-09 2013-09-09 20:45:00 2013-09-09
20:45:00
5 2013-09-09 21:00:00 2013-09-09 2013-09-09 2013-09-09 21:00:00 2013-09-09
21:00:00
6 2013-09-09 21:15:00 2013-09-09 2013-09-09 2013-09-09 21:15:00 2013-09-09
21:15:00
7 2013-09-09 21:30:00 2013-09-09 2013-09-09 2013-09-09 21:30:00 2013-09-09
21:30:00

Now I want to insert the minimum value of "Vollzeit" for each date in
"Zugnacht" into the colum "su" and the maximum value of "Vollzeit" for each
date in "Zugnacht" into the colum "sa". If I do it like this, it does
exactly what I want:

zn                   <- unique(treat$Zugnacht)
i=zn[1]
treat$su[treat$Zugnacht==as.POSIXct(i, "UTC")] <-
min(treat$Vollzeit[treat$Zugnacht==as.POSIXct(i, "UTC")])
treat$sa[treat$Zugnacht==as.POSIXct(i, "UTC")] <-
max(treat$Vollzeit[treat$Zugnacht==as.POSIXct(i, "UTC")])

This is the result for the first date in "Zugnacht":
head(treat)
             Vollzeit      Datum   Zugnacht                  su                 
sa
2 2013-09-09 20:15:00 2013-09-09 2013-09-09 2013-09-09 20:15:00 2013-09-10
04:30:00
3 2013-09-09 20:30:00 2013-09-09 2013-09-09 2013-09-09 20:15:00 2013-09-10
04:30:00
4 2013-09-09 20:45:00 2013-09-09 2013-09-09 2013-09-09 20:15:00 2013-09-10
04:30:00
5 2013-09-09 21:00:00 2013-09-09 2013-09-09 2013-09-09 20:15:00 2013-09-10
04:30:00
6 2013-09-09 21:15:00 2013-09-09 2013-09-09 2013-09-09 20:15:00 2013-09-10
04:30:00
7 2013-09-09 21:30:00 2013-09-09 2013-09-09 2013-09-09 20:15:00 2013-09-10
04:30:00


However I am not able to create a loop that runs over all 113 dates in zn. I
tried this, but it does not work. Can anybody help, please?

for (i in zn){
    treat$su[treat$Zugnacht==as.POSIXct(zn[i], "UTC")] <-
min(treat$Vollzeit[treat$Zugnacht==as.POSIXct(zn[i], "UTC")])
    treat$sa[treat$Zugnacht==as.POSIXct(zn[i], "UTC")] <-
max(treat$Vollzeit[treat$Zugnacht==as.POSIXct(zn[i], "UTC")])
    }

Kind regards
Christiane




--
View this message in context: http://r.789695.n4.nabble.com/how-di-I-write-a-for-loop-in-which-i-is-in-POSIX-tp4690211.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list