i is an element in zn so replace zn[i] with just i for (i in zn){ treat$su[treat$Zugnacht==as.POSIXct(i, "UTC")] <- min(treat$Vollzeit[treat$Zugnacht==as.POSIXct(zni, "UTC")]) treat$sa[treat$Zugnacht==as.POSIXct(zni, "UTC")] <- max(treat$Vollzeit[treat$Zugnacht==as.POSIXct(zni, "UTC")]) }
If you want to keep i as an index you need something like: for(i in 1:length(i)) { #do stuff on zn[i] } On Thu, 2014-05-08 at 14:19 -0700, peregrine wrote: > Hallo, > > I have a table in which I would like to insert the min and max values of > another column (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 column "su" and the maximum value of "Vollzeit" for each > date in "Zugnacht" into the column "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-to-write-a-loop-in-which-i-has-to-be-in-POSIX-format-tp4690212.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. ______________________________________________ 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.