Difference between revisions of "OpasnetUtils/Drafts"

From Testiwiki
Jump to: navigation, search
(Answer)
(Answer: timepoints improved)
Line 633: Line 633:
 
# Calculate the cumulative impact of the events on building stock to given years
 
# Calculate the cumulative impact of the events on building stock to given years
  
timepoints <- function(X, obsyear) {
+
timepoints <- function(
 
# Function timepoints takes an event list and turns that into existing crosscutting situations at  
 
# Function timepoints takes an event list and turns that into existing crosscutting situations at  
# timepoints defined by years.
+
# timepoints defined by years. The output will have index Time.
# X must be an ovariable with index Eventyear.
+
# In other words, this will integrate over obstime at specified timepoints.
# obsyear must be a vector of years.
+
X, # X must be an ovariable with a column of the same name as obstime.
 +
obstime # obstime must be a single-column data.frame of observation times.  
 +
# obstime and timecol may be numeric (by coercion) or POSIXt.
 +
) {
 +
timecol <- colnames(obstime)
 +
marginals <- colnames(X@output)[X@marginal]
  
if(is.factor(X@output$Eventyear))
+
# tapply (and therefore possibly oapply) changes continuous indices to factors! Must change back by hand.
X@output$Eventyear <- as.numeric(levels(X@output$Eventyear)[X@output$Eventyear])
+
if("factor" %in% c(class(obstime[[timecol]]), class(X@output[[timecol]]))) {
# tapply (and therefore oapply) changes continuous indices to factors! Must change back by hand.
+
X@output[[timecol]] <- as.numeric(as.character(X@output[[timecol]]))
 +
obstime[[timecol]] <- as.numeric(as.character(obstime[[timecol]]))
 +
}
  
 
out <- data.frame()
 
out <- data.frame()
  
for(i in obsyear) {
+
for(i in obstime[[timecol]]) {
out <- rbind(out, data.frame(
+
temp <- X@output[X@output[[timecol]] <= i , ]
Year = i,
+
temp <- aggregate(
X@output[X@output$Eventyear <= i , ]
+
temp[paste(X@name, "Result", sep = "")],
))
+
by = temp[colnames(temp) %in% setdiff(marginals, timecol)],
 +
FUN = sum
 +
)
 +
if(nrow(temp) > 0) out <- rbind(out, data.frame(Time = i, temp))
 
}
 
}
 
X@output <- out
 
X@output <- out
X@marginal <- c(TRUE, X@marginal) # Add Year to marginal
+
X@marginal <- colnames(out) %in% c("Time", marginals) # Add Time to marginal
X <- oapply(X, cols = "Eventyear", FUN = "sum", na.rm = TRUE)
+
 
X@output$Year <- as.numeric(as.character(X@output$Year))
 
X@output <- X@output[!is.na(result(X)) , ]
 
 
return(X)
 
return(X)
 
}
 
}

Revision as of 08:51, 14 February 2015



Question

Which functions are so useful that they should be taken into OpasnetUtils package? This page contains draft function which will be included when they are good enough and found important.

Answer

Call the objects stored by this code from another rode with this command:

objects.latest("Op_en6007", code_name = "answer")

+ Show code

See also

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>