File: index.R

package info (click to toggle)
r-zoo 1.8-14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,760 kB
  • sloc: ansic: 373; makefile: 2
file content (97 lines) | stat: -rw-r--r-- 1,962 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
index <- function(x, ...)
{
  UseMethod("index")
}

index.default <- function(x, ...)
{
  seq_len(NROW(x))
}

index.zoo <- function(x, ...)
{
  attr(x, "index")
}

index.ts <- function(x, ...)
{
  xtsp <- tsp(x)
  seq(xtsp[1], xtsp[2], by = 1/xtsp[3])
}

time.zoo <- function(x, ...)
{
  index(x)
}

"index<-" <- function(x, value) 
{
	UseMethod("index<-")
}

"time<-" <- function(x, value) 
{
	UseMethod("time<-")
}

"index<-.zoo" <- function(x, value) 
{
	if(length(index(x)) != length(value)) 
	  stop("length of index vectors does not match")
	if(is.unsorted(ORDER(value)))
	  stop("new index needs to be sorted")	
	attr(x, "index") <- value
	return(x)
}

"time<-.zooreg" <- "index<-.zooreg" <- function(x, value) 
{
	if(length(index(x)) != length(value)) 
	  stop("length of index vectors does not match")
	if(is.unsorted(ORDER(value)))
	  stop("new index needs to be sorted")	

        ## check whether new index still conforms with
	## frequency, if not: drop frequency
        d <- try(diff(as.numeric(value)), silent = TRUE)
	ok <- if(inherits(d, "try-error") || length(d) < 1 || anyNA(d)) FALSE
	else {	    
            deltat <- min(d)
	    dd <- d/deltat
	    if(identical(all.equal(dd, round(dd)), TRUE)) {	    
                freq <- 1/deltat
                if(freq > 1 && identical(all.equal(freq, round(freq)), TRUE)) freq <- round(freq)
  	        identical(all.equal(attr(x, "frequency") %% freq, 0), TRUE)
	    } else {
	        FALSE
	    }
	}
	if(!ok) {
	  attr(x, "frequency") <- NULL
	  class(x) <- class(x)[-which(class(x) == "zooreg")]
	}
 	
	attr(x, "index") <- value
	return(x)
}

"time<-.zoo" <- function(x, value) 
{
	if(length(index(x)) != length(value)) 
	  stop("length of time vectors does not match")
	attr(x, "index") <- value
	return(x)
}

start.zoo <- function(x, ...) 
{
	if (length(index(x)) > 0) index(x)[1]
	  else NULL
}

end.zoo <- function(x, ...) 
{
	lx <- length(index(x))
	if (lx > 0) index(x)[lx]
	  else NULL
}