[R] Help with programming a tricky algorithm

Rui Barradas ruipbarradas at sapo.pt
Mon Oct 22 17:33:23 CEST 2012


Hello,

In your original post, there was a column named 'country', it now seems 
to be 'name', therefore my function shouldn't work. To see the output of 
head(9 is helpfull but the better way is dput(). Try the following:

exmpl <- sub[, c("name", "idxy", "ix", iy")]
dput( head(exmpl, 30) )  # paste the output of this in a post


And also try to change the column name like I've said above. But a data 
example would really be needed.

Hope this helps,

Rui Barradas
Em 22-10-2012 16:26, Andrew Crane-Droesch escreveu:
> Rui,
>
> Thanks a lot for your help.  Unfortunately this doesn't work though:
>
> 1> is.border <- function(idx, DF){
> 1+     i1 <- DF$ix %in% (DF$ix[idx] + c(-1, 1)) & DF$iy == DF$iy[idx]
> 1+     i2 <- DF$iy %in% (DF$iy[idx] + c(-1, 1)) & DF$ix == DF$ix[idx]
> 1+     any(DF$country[idx] != DF$country[i1 | i2])
> 1+ }
> 1>
> 1> brdr <- sapply(sub$idxy, is.border, sub)
> 1> sub$border <- as.integer(brdr)
> 1> head(sub)
>          y         x    name year idxy idt     rain ndvi temp    pop95
> 1 10.05928 1.9315833   benin 2001 1892  66 112.9945 535.0 300.1728 
> 16.81851
> 2 10.05928 0.4770379    togo 2001  492  66 129.3910 464.5 300.3082 
> 24.74213
> 3 10.05928 1.0588560    togo 2001 1052  66 144.9040 460.5 300.2535 
> 29.34577
> 4 10.05928 2.8043106   benin 2001 2732  66 153.2175 572.5 300.1122 
> 20.39760
> 5 10.05928 3.8224924 nigeria 2001 3712  66 162.9701 589.5 300.0277 
> 11.40092
> 6 10.05928 0.8406742    togo 2001  842  66 143.1389 510.5 300.2740 
> 29.34577
>      pop00    pop05   cropland    pasture    rainl1 rainl2 templ1   
> templ2
> 1 19.20563 22.32065 0.04130662 0.02016802 112.05880 60.00279 303.4361 
> 304.4833
> 2 29.13643 33.08628 0.51615972 0.25667389 136.98332 38.74465 303.5708 
> 304.5079
> 3 34.55809 39.24327 0.25539863 0.09774620 129.25399 39.04244 303.5183 
> 304.5009
> 4 23.29283 27.07122 0.10509129 0.02065746 109.87102 119.92331 303.3055 
> 304.3630
> 5 13.16283 13.73924 0.12865660 0.61500187  99.13176 67.27786 303.1213 
> 304.1993
> 6 34.55809 39.24327 0.20321523 0.21327476 131.74456 38.64642 303.5380 
> 304.5035
>     pophat p96 p97 p98 p99 p00 p01 p02 p03 p04 p05 p06 nres ndvihat
> 1 19.82864   1   0   0   0   0   0   0   0   0   0   0 477.0112 576.8041
> 2 29.92640   1   0   0   0   0   0   0   0   0   0   0 -854.3601 486.1667
> 3 35.49513   1   0   0   0   0   0   0   0   0   0   0 -930.1263 487.2222
> 4 24.04851   1   0   0   0   0   0   0   0   0   0   0 363.3710 544.4345
> 5 13.27811   1   0   0   0   0   0   0   0   0   0   0 1234.5701 541.9867
> 6 35.49513   1   0   0   0   0   0   0   0   0   0   0 -153.9017 496.0020
>         res   ffresid       fe     wvec   ndviann   exrate isnigeria 
> time D
> 1  32.60776 -676.2175 429.5025 261.6586 0.9947269 1.421233 0    2 0
> 2  41.00458 -343.6485 350.6056 251.5608 1.1106229 1.398511 0    2 0
> 3  38.19903 -419.1147 349.4112 245.9921 1.0967285 1.398511 0    2 0
> 4 113.99665  450.3076 385.6136 257.4387 1.0747216 1.421233 0    2 0
> 5 126.52839  778.1297 390.0818 268.2091 1.1022806 5.254477 1    2 1
> 6  78.50062  227.3886 359.1096 245.9921 1.0719824 1.398511 0    2 0
>   timexD ix iy border
> 1      0 27  1      0
> 2      0  7  1      0
> 3      0 15  1      0
> 4      0 39  1      0
> 5      2 53  1      0
> 6      0 12  1      0
> 1> unique(sub$border)
> [1] 0
>
> On 10/21/2012 09:48 AM, Rui Barradas wrote:
>> 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) 
>




More information about the R-help mailing list