[R] Aggregating spatial data
David Carlson
dcarlson at tamu.edu
Tue Nov 26 16:42:15 CET 2013
The points are easy:
plot(y~x, w, pch=".", asp=1)
The four polygons are just four polygon() calls:
polygon(c(.5, .5, 50.5, 50.5), c(.5, 50.5, 50.5, .5))
polygon(c(.5, .5, 50.5, 50.5), c(50.5, 100.5, 100.5, 50.5))
polygon(c(50.5, 50.5, 100.5, 100.5), c(.5, 50.5, 50.5, .5))
polygon(c(50.5, 50.5, 100.5, 100.5), c(50.5, 100.5, 100.5,
50.5))
To make the process more generalizable (at least for square
blocks), try this:
block <- c(2, 2)
size <- 50
start <- c(.5, .5)
for (i in 0:(block[1]-1)) {
for (j in 0:(block[2]-1)) {
px <- start[1]+i*size
py <- start[2]+j*size
polygon(c(px, px, px+size, px+size),
c(py, py+size, py+size, py),
border="red")
}
}
David
-----Original Message-----
From: IOANNA [mailto:ii54250 at msn.com]
Sent: Tuesday, November 26, 2013 6:02 AM
To: dcarlson at tamu.edu
Cc: r-help at r-project.org
Subject: RE: [R] Aggregating spatial data
Fantastic. Thanks very much! Is there an easy way to plot the
points and
the 4 areas?
Best,
Ioanna
-----Original Message-----
From: David Carlson [mailto:dcarlson at tamu.edu]
Sent: 25 November 2013 15:21
To: 'IOANNA'; r-help at r-project.org
Subject: RE: [R] Aggregating spatial data
Something like this?
> s <- expand.grid(x=seq(1,100,by=1),y=seq(1,100,by=1))
> w <-
data.frame(x=s$x,y=s$y,z1=rep(c(1,2,3,4),times=length(s$x/4)),
+ z2=seq(1,length(s$x),by=1))
> w$EW <- cut(w$x, breaks=c(.5, 50.5, 100.5), labels=c("West",
"East"))
> w$NS <- cut(w$y, breaks=c(.5, 50.5, 100.5), labels=c("South",
"North"))
> aggregate(z1~EW+NS, w, table)
EW NS z1.1 z1.2 z1.3 z1.4
1 West South 2600 2600 2400 2400
2 East South 2400 2400 2600 2600
3 West North 2600 2600 2400 2400
4 East North 2400 2400 2600 2600
> table(w$z1)
1 2 3 4
10000 10000 10000 10000
> aggregate(z2~EW+NS, w, mean)
EW NS z2
1 West South 2475.5
2 East South 2525.5
3 West North 7475.5
4 East North 7525.5
-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352
-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of IOANNA
Sent: Monday, November 25, 2013 8:46 AM
To: r-help at r-project.org
Subject: [R] Aggregating spatial data
Importance: High
Hello all,
I have a data frame in the form:
s<-expand.grid(x=seq(1,100,by=1),y=seq(1,100,by=1))
w<-data.frame(x=s$x,y=s$y,z1=rep(c(1,2,3,4),times=length(s$x/4))
,z2=seq(1,le
ngth(s$x),by=1))
The w$x and w$y represent the location of points and z1 and z2
attributes
corresponding to these points.
My question is how to divide this area in 4 sub-areas of equal
points each
and produce the counts of z1= '1', '2' , '3' in each quarter as
well as mean
values of z2 for each quarter.
Best,
Ioanna
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible
code.
More information about the R-help
mailing list