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

arun smartpink111 at yahoo.com
Tue May 13 03:20:50 CEST 2014



Hi,
You can also try:
library(plyr)
treat3 <- ddply(treat, .(Zugnacht), mutate, su=min(Vollzeit), sa=max(Vollzeit))
identical(treat2[,c(2,1,3:4)],treat3)
#[1] TRUE

A.K.


On Monday, May 12, 2014 2:46 PM, "Adams, Jean" <jvadams at usgs.gov> wrote:
Christiane,

You will get replies to your queries more quickly if you include
reproducible code in your question.  For example, it would have been
helpful if you had posted the result returned by dput(head(treat)) rather
than just copying and pasting the first few rows of treat.

treat <- structure(list(Vollzeit = structure(c(1378775700, 1378776600,
1378777500, 1378778400, 1378779300, 1378780200), class = c("POSIXct",
"POSIXt"), tzone = ""), Zugnacht = structure(c(1378702800, 1378702800,
1378702800, 1378702800, 1378702800, 1378702800), class = c("POSIXct",
"POSIXt"), tzone = "")), .Names = c("Vollzeit", "Zugnacht"), class =
"data.frame", row.names = c(NA,
-6L))

You can use the ddply() function of the plyr package in R to calculate the
minimum and maximum Vollzeit for each value of Zugnacht.  Then you can
merge the result with the original data to have those minima and maxima
repeated for multiple rows.  For example:

library(plyr)
minmax <- ddply(treat, .(Zugnacht), summarize, su=min(Vollzeit),
sa=max(Vollzeit))
treat2 <- merge(treat, minmax, all=TRUE)

Jean



On Thu, May 8, 2014 at 4:12 PM, peregrine <halconperegrino at gmx.de> wrote:

> 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.
>
> ______________________________________________
> R-help at 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]]


______________________________________________
R-help at 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.



More information about the R-help mailing list