[R] Help with programming a tricky algorithm

Andrew Crane-Droesch andrewcd at gmail.com
Mon Oct 22 17:51:43 CEST 2012


Hi Rui,

Thanks for the quick reply!  It was my mistake not to notice $country in 
thr fourth line of your code.  I went back and changed it to $name, and 
got the following output when I mapped the borders:

http://i.imgur.com/DQ3IB.png

Here is the output of your function:

1> exmpl <- sub[, c("name", "idxy", "ix", "iy")]
1> dput( head(exmpl, 30) )
structure(list(name = structure(c(1L, 6L, 6L, 1L, 5L, 6L, 1L,
1L, 1L, 1L, 1L, 6L, 1L, 1L, 1L, 5L, 1L, 6L, 1L, 5L, 6L, 1L, 1L,
3L, 1L, 6L, 1L, 6L, 1L, 3L), .Label = c("benin", "burkinafaso",
"ghana", "niger", "nigeria", "togo"), class = "factor"), idxy = c(1892L,
492L, 1052L, 2732L, 3712L, 842L, 2522L, 3292L, 3012L, 3432L,
2172L, 772L, 2242L, 3152L, 1402L, 3852L, 1612L, 422L, 2662L,
3642L, 702L, 2942L, 2802L, 212L, 1682L, 1122L, 1962L, 1192L,
2452L, 142L), ix = c(27, 7, 15, 39, 53, 12, 36, 47, 43, 49, 31,
11, 32, 45, 20, 55, 23, 6, 38, 52, 10, 42, 40, 3, 24, 16, 28,
17, 35, 2), iy = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = c("name",
"idxy", "ix", "iy"), row.names = c(NA, 30L), class = "data.frame")

I must say that I am impressed by the response of the community on this 
mailing list.  I look forward to getting better at R such that I can be 
more useful to others myself!

Best,
Andrew

On 10/22/2012 08:33 AM, Rui Barradas wrote:
> 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