File: basic.R

package info (click to toggle)
r-cran-spacetime 1.2-4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,308 kB
  • sloc: sh: 13; makefile: 2
file content (182 lines) | stat: -rw-r--r-- 5,356 bytes parent folder | download | duplicates (3)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
options(digits=5)
options(xts_check_TZ=FALSE)
###################################################
### chunk number 4: 
###################################################
library(sp)
library(spacetime)
set.seed(13579) # to make outcome a bit predictable!
sp = cbind(x = c(0,0,1), y = c(0,1,1))
row.names(sp) = paste("point", 1:nrow(sp), sep="")
sp = SpatialPoints(sp)
time = as.POSIXct("2010-08-05", tz="GMT")+3600*(10:13)
m = c(10,20,30) # means for each of the 3 point locations
mydata = rnorm(length(sp)*length(time),mean=rep(m, 4))
IDs = paste("ID", 1:length(mydata), sep = "_")
mydata = data.frame(values = signif(mydata,3), ID=IDs)
stfdf = STFDF(sp, time, mydata)


###################################################
### chunk number 5: 
###################################################
format(as.data.frame(stfdf, row.names = IDs), tz="GMT")
format(as(stfdf, "data.frame")[1:4,], tz="GMT")

###################################################
### chunk number 6: 
###################################################
unstack(stfdf)
t(unstack(stfdf))
unstack(stfdf, which = 2)

###################################################
### chunk number 7: 
###################################################
as(stfdf[,,1], "xts")
as(stfdf[,,2], "xts")

###################################################
### chunk number 8: 
###################################################
stfdf[[1]]
stfdf[["values"]]
stfdf[["newVal"]] <- rnorm(12)
stfdf$ID
stfdf$ID = paste("OldIDs", 1:12, sep="")
stfdf$NewID = paste("NewIDs", 12:1, sep="")
stfdf


###################################################
### chunk number 9: 
###################################################
stfdf[,1] # SpatialPointsDataFrame:
stfdf[,,1]
stfdf[1,,1] # xts
stfdf[,,"ID"]
stfdf[1,,"values", drop=FALSE] # stays STFDF:
stfdf[,1, drop=FALSE] #stays STFDF

###################################################
### chunk number 10: 
###################################################
showClass("STSDF")

###################################################
### chunk number 11: 
###################################################
showClass("STIDF")
sp = expand.grid(x = 1:3, y = 1:3)
row.names(sp) = paste("point", 1:nrow(sp), sep="")
sp = SpatialPoints(sp)
time = as.POSIXct("2010-08-05", tz="GMT")+3600*(11:19)
m = 1:9 * 10 # means for each of the 9 point locations
mydata = rnorm(length(sp), mean=m)
IDs = paste("ID",1:length(mydata))
mydata = data.frame(values = signif(mydata,3),ID=IDs)
stidf = STIDF(sp, time, mydata)
stidf


###################################################
### chunk number 12: 
###################################################
stidf[1:2,]


###################################################
### chunk number 13: 
###################################################
stfdf[,time[3]]


###################################################
### chunk number 14: 
###################################################
class(stfdf[,time[3],drop=FALSE])

###################################################
### chunk number 15: 
###################################################
stfdf[1, , "values"]

###################################################
### chunk number 16: 
###################################################
class(stfdf[1,drop=FALSE])


###################################################
### chunk number 17: 
###################################################
class(stfdf)
class(as(stfdf, "STSDF"))
class(as(as(stfdf, "STSDF"), "STIDF"))
class(as(stfdf, "STIDF"))


###################################################
### chunk number 18: 
###################################################
x = as(stfdf, "STIDF")
class(as(x, "STSDF"))
class(as(as(x, "STSDF"), "STFDF"))
class(as(x, "STFDF"))
xx = as(x, "STFDF")
identical(stfdf, xx)

stsdf = as(stfdf, "STSDF")
stsdf[[1]]
stsdf[["values"]]
stsdf[["newVal"]] <- rnorm(12)
stsdf$ID
stsdf$ID = paste("OldIDs", 1:12, sep="")
stsdf$NewID = paste("NewIDs", 12:1, sep="")
stsdf
stsdf[,1] # SpatialPointsDataFrame:
stsdf[,,1]
stsdf[1,,1] # xts
stsdf[,,"ID"]
stsdf[1,,"values", drop=FALSE] # stays STIDF:
stsdf[,1, drop=FALSE] #stays STIDF
format(as.data.frame(stsdf), tz="GMT")
format(as(stsdf, "data.frame"), tz="GMT")

stidf = as(stfdf, "STIDF")
stidf[[1]]
stidf[["values"]]
stidf[["newVal"]] <- rnorm(12)
stidf$ID
stidf$ID = paste("OldIDs", 1:12, sep="")
stidf$NewID = paste("NewIDs", 12:1, sep="")
stidf
stidf[,1] # SpatialPointsDataFrame:
stidf[,,1]
stidf[1,,1] # xts
stidf[,,"ID"]
stidf[1,,"values", drop=FALSE] # stays STIDF:
stidf[,1, drop=FALSE] #stays STIDF
format(as.data.frame(stidf), tz="GMT")
format(as(stidf, "data.frame"), tz="GMT")

sp = cbind(x = c(0,0,1), y = c(0,1,1))
row.names(sp) = paste("point", 1:nrow(sp), sep="")
sp = SpatialPoints(sp)
library(xts)
time = xts(1:4, as.POSIXct("2010-08-05", tz="GMT")+3600*(10:13))
mydata = rnorm(length(sp)*length(time),mean=rep(m, 4))
IDs = paste("ID", 1:length(mydata), sep = "_")
mydata = data.frame(values = signif(mydata,3), ID=IDs)
spx = SpatialPointsDataFrame(sp, data.frame(values = c("a", "b", "c")))
try(stfdf <- STFDF(spx, time, mydata))
timex = time
names(timex) = "values"
try(stfdf <- STFDF(sp, timex, mydata))
mydatax = mydata
names(mydatax) = "x"
try(stfdf <- STFDF(spx, timex, mydatax))
stfdf = STFDF(sp, timex, mydatax)
try(stfdf[["values"]] <- 1:12)
stfdf = STFDF(spx, time, mydatax)
try(stfdf[["values"]] <- 1:12)