Generate output based on multiple criteria

I have 2 data frames for 2 stacks that give information about potential emissions. One data frame gives the time frame of what hours the system turns on and off for 4 seasons. Each season begins on a specific date. The second file gives me stack information.

I'm trying to use some sample file to check how to do this, and so far I have managed to create a function following the example that allows me to create a data frame with the dates I need and a column with seasons for each date. I am really struggling with the programming concept to figure out how to combine 3 data frames to create an output template that I am trying to customize.

To show you an example of my input:

Stack_info File:

enter image description here

An example of a seasonal profile that shows when the system is on or off:

enter image description here

and the conclusion that I should be to create data frames for each year in the following format (only black font and red text to just explain what these values ​​are):

enter image description here

The hardest thing that I find is that my output files for each year will have a unique first line, and the second line will be repeated for each pollutant. and from the 3rd row - hourly data for all 8760 hours. This must be repeated for the next contaminant.

So far, I have managed to create a function that helps me set the season every day of the year. For example:

#function to create seasons
d = function(month_day) which(lut$month_day == month_day)
lut = data.frame(all_dates = as.POSIXct("2012-1-1") + ((0:365) * 3600 * 24),
                 season = NA)
lut = within(lut, { month_day = strftime(all_dates, "%b-%d") })
lut[c(d("Jan-01"):d("Mar-15"), d("Nov-08"):d("Dec-31")), "season"] = "winter"
lut[c(d("Mar-16"):d("Apr-30")), "season"] = "spring"
lut[c(d("May-01"):d("Sep-27")), "season"] = "summer"
lut[c(d("Sep-28"):d("Nov-07")), "season"] = "autumn"
rownames(lut) = lut$month_day

## create date data frame and assign seasons
dates = data.frame(dates =seq(as.Date('2010-01-01'),as.Date('2012-12-31'),by = 1))

 dates = within(dates, { 
  season =  lut[strftime(dates, "%b-%d"), "season"] 
})

This gives me a date data frame, and my other 2 frames are sample data (as shown):

structure(list(`Source no` = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Source = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L), .Label = c("Stack 1", "Stack 2"), class = "factor"), 
    Period = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Day = structure(c(2L, 
    6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L, 
    7L, 5L, 1L, 3L, 4L), .Label = c("Fri", "Mon", "Sat", "Sun", 
    "Thu", "Tue", "Wed"), class = "factor"), `Spring On` = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 15L, 
    15L, 15L, 15L, 15L, 15L, 15L), `Spring Off` = c(23L, 23L, 
    23L, 23L, 23L, 23L, 23L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 18L, 
    18L, 18L, 18L, 18L, 18L, 18L), `Summer On` = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "off", class = "factor"), `Summer Off` = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "off", class = "factor"), `Autumn On` = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "off", class = "factor"), `Autumn Off` = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "off", class = "factor"), `Winter On` = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L), .Label = c("0", "off"), class = "factor"), 
    `Winter Off` = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("23", 
    "off"), class = "factor")), .Names = c("Source no", "Source", 
"Period", "Day", "Spring On", "Spring Off", "Summer On", "Summer Off", 
"Autumn On", "Autumn Off", "Winter On", "Winter Off"), class = "data.frame", row.names = c(NA, 
-21L)) -> profile

structure(list(SNAME = structure(1:2, .Label = c("Stack 1", "Stack 2"
), class = "factor"), ISVARY = c(1L, 4L), VELVOL = c(1L, 4L), 
    TEMPDENS = c(0L, 2L), `DUM 1` = c(999L, 999L), `DUM 2` = c(999L, 
    999L), NPOL = c(2L, 2L), `EXIT VEL` = c(26.2, 22.4), TEMP = c(341L, 
    328L), `STACK DIAM` = c(1.5, 2.5), W = c(0L, 15L), Nox = c(39, 
    33.3), Sox = c(15.5, 17.9)), .Names = c("SNAME", "ISVARY", 
"VELVOL", "TEMPDENS", "DUM 1", "DUM 2", "NPOL", "EXIT VEL", "TEMP", 
"STACK DIAM", "W", "Nox", "Sox"), class = "data.frame", row.names = c(NA, 
-2L)) -> stack_info

- - , , , , , 2010, 2011 2012 .

+4
1

. , .

, , ( n). DataFrame, :

d.out = data.frame(dates = seq(from=as.POSIXct("2010-01-01"), by=3600, to= as.POSIXct("2010-12-31")))
d.out$year = as.numeric(format(d.out$dates, "%Y"))
d.out$month = as.numeric(format(d.out$dates, "%m"))
d.out$day = as.numeric(format(d.out$dates, "%d"))
d.out$hour = as.numeric(format(d.out$dates, "%H"))
d.out$weekday = as.character(format(d.out$dates, "%a"))
d.out$doj = as.numeric(format(d.out$dates, "%j"))
d.out$season = "Winter"
d.out$season[d.out$doj >= 75 & d.out$doj < 121] = "Spring"
d.out$season[d.out$doj >= 121 & d.out$doj < 271] = "Summer"
d.out$season[d.out$doj >= 271 & d.out$doj < 312] = "Autumn"

, . -df :

library(dplyr)
library(tidyr)

profile_new =
profile %>%
    gather(season, hour, -c(`Source no`, Source, Period, Day)) %>%
    extract(season, c("season", "status"), "(\\w+?)\\s(\\w+)") %>%
    filter(hour != "off") %>%
    mutate(Day = as.character(Day), hour=as.numeric(hour)) %>%
    spread(status, hour)

, :

d.out %>%
    inner_join(profile_new, by=c("weekday"="Day", "season"="season")) %>%
    group_by(Source, dates, year, day, weekday, season, hour) %>%
    summarise(status = any(hour >= On & hour <= Off)) %>%
    inner_join(stack_info, by=c("Source"="SNAME")) %>%
    mutate(Nox = ifelse(status, Nox, 0),
             Sox = ifelse(status, Sox, 0)) %>%
    arrange(Source, year, dates, hour) %>%
    select(Source, year, day, weekday, season, hour, `EXIT VEL`, TEMP, `STACK DIAM`, W, Nox, Sox)

, , . csv ( append = TRUE).

+2

Source: https://habr.com/ru/post/1607367/


All Articles