Difference between revisions of "OpasnetUtils/Drafts"

From Testiwiki
Jump to: navigation, search
(Answer: timing added)
(Answer)
Line 404: Line 404:
 
}
 
}
  
findrest <- function (X, cols, total = 1)
 
{
 
# findrest input is an ovariable that can be integrated over indices cols to result in total.
 
# Often it is used with uncertain fractions. The largest fraction is omitted or NA, and it is replaced by whatever is missing from the total.
 
if (nrow(X@output) == 0)
 
X <- EvalOutput(X)
 
rescol <- comment(result(X))
 
 
# temp is the amount that is still missing from the total.
 
temp <- total - oapply(X, cols = cols, FUN = function(x) sum(x, na.rm = TRUE))
 
temp@output[[rescol]] <- NULL # Remove old rescol because it would cause trouble later.
 
out <- merge(X, temp)@output
 
 
# Replace missing values with values from temp.
 
out[[rescol]] <- ifelse(is.na(out[[rescol]]), out$Result, out[[rescol]]) # Result comes from temp
 
if(rescol != "Result") out$Result <- NULL
 
X@output <- out
 
return(X)
 
}
 
 
findrest <- function (X, cols, total = 1)
 
findrest <- function (X, cols, total = 1)
 
{
 
{
Line 478: Line 459:
 
}
 
}
  
timing <- function(x, tz = "EET")  
+
timing <- function(dat, timecol = NA, weeks = 52, tz = "EET")  
 
# timing converts character or numeric inputs into times using a few default formats
 
# timing converts character or numeric inputs into times using a few default formats
 
{
 
{
x <- as.character(x)
+
if(is.data.frame(dat)) times <- tolower(dat[[timecol]]) else times <- dat
 +
 
 +
weekdays <- data.frame(
 +
Name = c("su", "ma", "ti", "ke", "to", "pe", "la",
 +
"sun", "mon", "tue", "wed", "thu", "fri", "sat",
 +
"sön", "mon", "tis", "ons", "tor", "fre", "lör"),
 +
Number = rep(0:6, times = 3)
 +
)
 +
 +
# day is the number of the weekday in case of repeating events.
 +
 
 +
day <- rep(NA, length(times))
 +
for(i in 1:nrow(weekdays))
 +
{
 +
temp1 <- grepl(weekdays$Name[i], times) # Is any weekday mentioned in times?
 +
day <- ifelse(is.na(day) & temp1, weekdays$Number[i], day) # Find the weekday number.
 +
}
 +
 
 +
# repeats are repeating events, x are non-repeating events.
 +
 +
repeats <- times[!is.na(day)]
 +
repeatsrow <- (1:length(times))[!is.na(day)]
 +
x <- times[is.na(day)]
 +
xrow <- (1:length(times))[is.na(day)]
 +
 +
########### First, change non-repeating timedates.
 +
# Change timedate into POSIXct assuming formats 15.3.2013 or 2013-03-15 and 15:24 or 15.24.
 +
# Note! 13 and 2013 mean 1.1.2013 and 3.2013 means 1.3.2013
 +
 
x <- ifelse(grepl("^[0-9][0-9]$", x), paste("20", x, sep = ""), x)
 
x <- ifelse(grepl("^[0-9][0-9]$", x), paste("20", x, sep = ""), x)
 
x <- ifelse(grepl("^[0-9][0-9][0-9][0-9]$", x), paste("1.1.", x, sep = ""), x)
 
x <- ifelse(grepl("^[0-9][0-9][0-9][0-9]$", x), paste("1.1.", x, sep = ""), x)
Line 495: Line 504:
 
x <- ifelse(!is.na(x), x, as.POSIXct(temp, format = "%d.%m.%y", tz = tz))
 
x <- ifelse(!is.na(x), x, as.POSIXct(temp, format = "%d.%m.%y", tz = tz))
  
return(as.POSIXct(x, origin = "1970-01-01", tz = tz))
+
x <- data.frame(
 +
Datrow = xrow,
 +
Timevalues = as.POSIXct(x, origin = "1970-01-01", tz = tz) # Intermediate values turn into numeric, therefore turned back to POSIX.
 +
)
 +
 +
############## Then, change weekly repeating timedates.
 +
 +
# repeats must have format ma 9:00 or TUESDAY 8.24. Weekday is case-insensitive and can be abbrevieated.
 +
 
 +
reptime <- gsub("[[:alpha:] ]", "", repeats) # Remove alphabets and spaces.
 +
reptime <- gsub("\\.", ":", reptime)
 +
reptime <- strsplit(reptime, split = ":")
 +
 
 +
temp2 <- numeric()
 +
for(i in 1:length(reptime))
 +
{
 +
temp2[i] <- as.numeric(reptime[[i]][1]) * 3600 + as.numeric(reptime[[i]][2]) * 60
 +
}
 +
 
 +
reptime <- temp2
 +
 
 +
starting <-  as.POSIXlt(paste(format(Sys.Date(), format = "%Y"), "-01-01", sep = "")) # First day of year.
 +
starting$mday <- starting$mday - as.numeric(format(starting, format = "%w")) + day[!is.na(day)] # Previous Sunday plus weekdaynumber.
 +
 
 +
reps <- data.frame()
 +
temp3 <- starting
 +
 
 +
for(i in 1:weeks)
 +
{
 +
temp3$mday <- temp3$mday + 7 # Make a weekly event.
 +
reps <- rbind(reps, data.frame(
 +
Datrow = repeatsrow,
 +
Timevalues = as.POSIXct(temp3, tz = "Europe/Helsinki") + reptime
 +
))
 +
}
 +
 
 +
out <- rbind(x, reps)
 +
 +
if(is.data.frame(dat))
 +
{
 +
dat$Datrow <- 1:nrow(dat)
 +
out <- merge(dat, out)
 +
out <- out[!colnames(out) %in% c("Datrow", timecol)]
 +
} else {
 +
out <- out$Timevalues
 +
}
 +
 +
return(out)
 
}
 
}
  

Revision as of 20:44, 1 February 2014



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>