0

I am using the package spatstat. I have a data frame of coordinates that I have divided into two sets, healthy(Mark=no) and diseased(Mark=yes). I am able to find the distance between the diseased point and all of the other points:

>D<-crossdist(diseased,healthy)

But now I need to remark my points based on the distances they are from the diseased point, sort of like:

>i<-length(D)
>for n=1:i,
>   if D[n]<1,
>       mark(n)<-yes,
>   else,
>       mark(n)<-no,
>   end
>end

I am new to using loops in R, and I was never really good at it in Matlab, I am still going through all of the help guides, but any tips on how to do this would be very helpful.

D looks like:

> D
              [,1]     [,2]     [,3]     [,4]    [,5]     [,6]     [,7]     [,8]
    [1,] 0.4796548 5.906068 6.061941 5.837476 5.62358 5.196297 7.687075 1.740198
             [,9]    [,10]    [,11]    [,12]    [,13]    [,14]    [,15]    [,16]
    [1,] 2.760947 8.921383 10.64188 8.385258 6.788703 6.542282 8.631057 11.19689
            [,17]    [,18]    [,19]    [,20]    [,21]    [,22]    [,23]    [,24]
    [1,] 11.38957 11.16738 10.88189 10.48931 9.116005 7.757465 7.907191 8.453458
            [,25]   [,26]    [,27]    [,28]    [,29]    [,30]    [,31]   [,32]
    [1,] 7.072456 6.89453 8.900875 8.613843 8.569073 11.82978 11.68813 9.27332
            [,33]   [,34]    [,35]    [,36]    [,37]    [,38]    [,39]
    [1,] 9.318501 9.64491 9.264779 9.357388 9.488624 10.57165 9.173374
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • there are numerous ways to approach this including a vectorized solution, however with out seeing a little bit of what `D` looks like (some people are not familiar with `spatstat`) it's hard to give precise information. I believe that the function `ifelse` may be helpful if D is a vector. Maybe show what D looks like with `head(D, 10) and post that. – Tyler Rinker Jul 24 '12 at 19:31
  • D is just a matrix with 1 row and 39 columns, 1 for each of the distances – user1549252 Jul 24 '12 at 19:52
  • Use `dput(D)` and paste the output. – Ryogi Jul 24 '12 at 22:28
  • It looks like all your values are > 1 but the vectorized version of your pseudo-code would be `ifelse(D < 1, 'yes', 'no')`. – Justin Jul 24 '12 at 22:29

1 Answers1

1

Two approaches to do this problem if you truly have a matrix (it appears you do):

set.seed(10)
D <- matrix(rnorm(20) + 2, 1)

D2 <- ifelse(D > 1, "yes", "no")       #method 1
D2

D2 <- matrix(rep("no", ncol(D)), 1)    #method 2
D2[D > 1] <- "yes"
D2

Edit:

D2 <- ifelse(c(D) > 1, "yes", "no")       #method 1

D2 <- matrix(rep("no", ncol(D)), 1)    #method 2
D2[D > 1] <- "yes"
c(D2)
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • If this is indeed what the OP wants to do, and D is large, method 2 will be faster. However, my guess is that `mark<-` is a function in spatstat. – GSee Jul 25 '12 at 01:34
  • yeah I thought that may be a possibility too and looked. `marks` is a function but I didn't see mark. I don't use the package so I don't know. – Tyler Rinker Jul 25 '12 at 02:03
  • So ifelse(D < 1, 'yes', 'no') did work to give me a matrix of yes or no for if the point was within the specified distance. Now I just have to figure out how to change the marks, marks meaning if the point is diseased (yes) or healthy(no), of the points in my data frame and then update the plot. Thank you for your help! – user1549252 Jul 25 '12 at 02:20
  • Really, now I just need to figure out how to change the matrix it spits out to be one column and rows for each point. then I think I can make a new ppp with the new marks(i.e. the matrix/table outputted). Any ideas on how to change the columns and rows? – user1549252 Jul 25 '12 at 02:55
  • Thank you, the c(D2) did work. I am still having problems creating a new ppp with the marks=c(D2), but I will go back over the help guide, seeing as I did not even know that simply c() would do what I needed. So there must be a simple solution to updating my ppp. – user1549252 Jul 25 '12 at 03:14
  • I got it to work by changing it to an array: D<-crossdist(diseased,healthy) a<-ifelse(D < 1, "yes", "no") A<-c(a) #changes to each number being a row instead of column b<-ifelse(D<3, 'yes', 'no') B<-c(b) c<-ifelse(D<6, 'yes','no') C<-c(c) newmarks.A<-array(A,dim=c(length(A),1)) newmarks.B<-array(B,dim=c(length(B),1)) newmarks.C<-array(C,dim=c(length(C),1)) HI17mfav attach(HI17mfav) distless1.P<-ppp(x,y,c(-1,10),c(-1,10),marks=newmarks.A) distless3.P<-ppp(x,y,c(0,10),c(0,10),marks=newmarks.B) distless6.P<-ppp(x,y,c(0,10),c(0,10),marks=newmarks.C) – user1549252 Jul 25 '12 at 13:57