[R-sig-Geo] Create/convert a point pattern from raw txt file in spatstat?
Barry Rowlingson
b.rowlingson at lancaster.ac.uk
Tue Oct 14 19:47:54 CEST 2008
2008/10/14 <opheliawang at mail.utexas.edu>:
> Hi all,
>
> I'm new to R, so I'm still learning how to do this: I have a txt file that
> contains X Y coordinate of trees (range from 0-100 for both X and Y) and a
> "mark" column that's the tree ID. The total is about 600 lines. Part of the
> data looks like:
>
> X Y Mark
> 1 94 1
> 2.5 94.5 4
> 3 93 5
> 4 92 6
> 4 95 7
> 6 98 8
> 4 84 9
> 6 86 10
> 6 86 11
> 6 86 12
> 6 86 13
> 8 88 14
> 9.5 89.5 15
> 10 90 16
>
> In order to create a point pattern, should I used scanpp, ppp, or
> as.ppp?Here's the error I get for each command:
>
>> test <- read.table("C:/dissertation/data2006/Parcela_1-3/Juyuin_tree.txt")
>> w <- owin(c(0,100),c(0,100))
>> as.ppp(test, w, fatal=TRUE)
>
> Error: is.numeric(x) is not TRUE
That's a clue there. Something in your data isn't a number. Now your
file looks like all numbers.. except... those headers!
You need to tell read.table that you've got headings, by using
head=TRUE. Compare:
> test = read.table("tree.txt")
> test[1:5,]
V1 V2 V3 <- R made these headings
1 X Y Mark <- oops
2 1 94 1
3 2.5 94.5 4
4 3 93 5
5 4 92 6
> test = read.table("tree.txt",head=TRUE)
> test[1:5,]
X Y Mark <- R's headings
1 1.0 94.0 1 <- your data starts here
2 2.5 94.5 4
3 3.0 93.0 5
4 4.0 92.0 6
5 4.0 95.0 7
from there, as.ppp(test,w) works fine:
> as.ppp(test, w, fatal=TR)
marked planar point pattern: 14 points
marks are numeric, of type 'integer'
window: rectangle = [0, 100] x [0, 100] units
Warning message:
In ppp(X[, 1], X[, 2], window = win, check = check) :
data contain duplicated points
If you still get errors with head=TRUE then there's something else in
your data file that's not a number. Read some introductory R docs on
reading data (help(read.table) for starters).
summary(test) and str(test) will tell you things about test before
you go on and try and do things with it. You should always make sure
step N is okay before assuming the problem is with step N+1...
Barry
More information about the R-sig-Geo
mailing list