[R] Help with programming a tricky algorithm

Rui Barradas ruipbarradas at sapo.pt
Sun Oct 21 18:48:42 CEST 2012


Hello,

Thanks for the dataset, Arun, I could test my function and it was still 
wrong (apologies to the op).
Now I think I've got it.

is.border <- function(idx, DF){
     i1 <- DF$ix %in% (DF$ix[idx] + c(-1, 1)) & DF$iy == DF$iy[idx]
     i2 <- DF$iy %in% (DF$iy[idx] + c(-1, 1)) & DF$ix == DF$ix[idx]
     any(DF$country[idx] != DF$country[i1 | i2])
}

brdr <- sapply(Mydata$idxy, is.border, Mydata)
Mydata$border <- as.integer(brdr)


Tested it and it all went allright.

Hope this helps,

Rui Barradas

Em 21-10-2012 15:36, arun escreveu:
> HI,
> I am not sure whether this is what you want.
> Mydata<-read.table(text="
>   idxy    ix    iy    country    col5
>   1        1    1    c1            x1
>   2        1    2    c1            x2
>   3        1    3    c1            x3
>   4        2    4    c1            x4
>   5        2    4    c2            x5
>   6        2    5    c2            x6
>   7        3    5    c3            x7
>   8        3    5    c3            x8
>   9        3    5    c3            x9
> ",sep="",stringsAsFactors=FALSE,header=TRUE)
> country1<-as.numeric(gsub(".*(\\d+)","\\1",Mydata$country))
>   Mydata1<-data.frame(ix=abs(diff(c(Mydata$ix[2],Mydata$ix))),iy=abs(diff(c(Mydata$iy[2],Mydata$iy))),country=abs(diff(c(country1[2],country1))))
>   Mydata2<- Mydata[apply(Mydata1,1,function(x) all(!duplicated(x)|!duplicated(x,fromLast=TRUE))),]
>   Mydata2$border<-1
> res<-merge(Mydata,Mydata2,by.x=c("idxy","ix","iy","country","col5"),by.y=c("idxy","ix","iy","country","col5"),all.x=TRUE)
>   res
> #  idxy ix iy country col5 border
> #1    1  1  1      c1   x1      1
> #2    2  1  2      c1   x2      1
> #3    3  1  3      c1   x3      1
> #4    4  2  4      c1   x4      1
> #5    5  2  4      c2   x5      1
> #6    6  2  5      c2   x6      1
> #7    7  3  5      c3   x7      1
> #8    8  3  5      c3   x8     NA
> #9    9  3  5      c3   x9     NA
>
>
>
>
>
> ----- Original Message -----
> From: Andrew Crane-Droesch <andrewcd at gmail.com>
> To: r-help at r-project.org
> Cc:
> Sent: Saturday, October 20, 2012 3:36 AM
> Subject: [R] Help with programming a tricky algorithm
>
> Hi All,
>
> I'm a little stumped by the following problem.  I've got a dataset with the following structure:
>
> idxy    ix    iy    country    (other variables)
> 1        1    1    c1            x1
> 2        1    2    c1            x2
> 3        1    3    c1            x3
> .        .        .       .            .
>
> 3739    55    67    c7        x3739
> 3740    55    68    c7        x3740
>
> where ix and iy are interger-valued indices of the actual x and y coordinates for the gridded data
>
> I want to define a "border" variable that equals 1 if the cell north, east, west, or south of it has a different value of the country variable.  So, for the row with idxy = 1, border would equal 1 if there is any idxy with country !=c1 and ix = 2 (or zero) or iy = 2 (or zero).
>
> Any thoughts?
>
> Thanks!
> Andrew
>
> ______________________________________________
> 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.
>
>
> ______________________________________________
> 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