使用3列计算连续日期和相应值之间的最大日期

时间:2017-02-21 13:01:38

标签: r datetime

我有这些数据:

import tkinter as tk   # python3
TITLE_FONT = ("Helvetica", 18, "bold")

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is the start page", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Go to Page One",
                            command=lambda: controller.show_frame("PageOne"))
        button2 = tk.Button(self, text="Go to Page Two",
                            command=lambda: controller.show_frame("PageTwo"))
        button1.pack()
        button2.pack()


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 1", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 2", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

我需要2个新列df $ MaxWind(根据持续时间包含Wind的最大值)和df $ MaxDates(包含最大风的相应日期),如下所示:

 df<-airquality[1:30,]
 df$duration<- rep(c(1,NA,0,2,0,0,3,0,NA,1),3)
 df$Year<- rep(1900,30)
 df$Dates <-as.Date(ISOdate(df$Year,df$Month, df$Day))

我们可以在持续时间内重复MaxWind和Maxdates(NA或O),或者像我这样用NA替换它。

我尝试使用 Ozone Solar.R Wind Temp Month Day duration Year Dates MaxWind MaxDates 41 190 7.4 67 5 1 1 1900 1900-05-01 7.4 1900-05-01 36 118 8.0 72 5 2 NA 1900 1900-05-02 NA NA 12 149 12.6 74 5 3 0 1900 1900-05-03 NA NA 18 313 11.5 62 5 4 2 1900 1900-05-04 14.3 1900-05-05 NA NA 14.3 56 5 5 0 1900 1900-05-05 NA NA 28 NA 14.9 66 5 6 0 1900 1900-05-06 NA NA 23 299 8.6 65 5 7 3 1900 1900-05-07 20.1 1900-05-09 19 99 13.8 59 5 8 0 1900 1900-05-08 NA NA 8 19 20.1 61 5 9 NA 1900 1900-05-09 NA NA 命令,但我不知道如何影响风的最大值。

pmax

我收到错误:

  

if(empty(.data))返回(.data)时出错:   valeurmanquantelàoùTRUE/ FALSE est requis

1 个答案:

答案 0 :(得分:1)

您可以尝试以下操作:

library(plyr)

maxWindDate <- function(row) {
  in.range <- row$Dates <= df$Dates & df$Dates - row$duration < row$Dates
  winds <- df$Wind[in.range]
  dates <- df$Dates[in.range]
  if (length(winds) == 0) {
    data.frame(maxWind=NA, maxDate=NA)
  } else {
    maxWind <- max(winds)
    maxDate <- dates[which(winds == maxWind)[1]]
    data.frame(maxWind=maxWind, maxDate=maxDate)
  }
}

df <- adply(df, 1, maxWindDate)