Hi, thanks a lot, now it works perfectly! I’v made a few changes in the syntax using your suggestion:

#define sectors
for (i in 1:n) 
{
  if ( !(is.na (varofint[[i]]) | is.na (varlag[[i]] )))  {
    if (varofint[[i]]>=m.varofint & varlag[[i]]>=m.varlag) sec[i] <- 1
    if (varofint[[i]]<m.varofint & varlag[[i]]<m.varlag) sec[i] <- 2
    if (varofint[[i]]<m.varofint & varlag[[i]]>=m.varlag) sec[i] <- 3
    if (varofint[[i]]>=m.varofint & varlag[[i]]<m.varlag) sec[i] <- 4
  }
}
# Define colors for sectors
sec.all <- sec
colors1 <- c(1:n)
for (i in 1:n) {
  if (sec.all[i]==1) colors1[i] <- "red"
  if (sec.all[i]==2) colors1[i] <- "blue"
  if (sec.all[i]==3) colors1[i] <- "lightblue"
  if (sec.all[i]==4) colors1[i] <- "pink"
  if (sec.all[i]==0) colors1[i] <- "white"
  if (sec.all[i]>4) colors1[i] <- "white"
}
#
# Mark all non-significant & NAs regions white
loc.m.data <- sec*vec
colors2 <- colors1
for (i in 1:n) 
  {
  if ( !(is.na (loc.m.data[i])) )  {
  if (loc.m.data[i]==0) colors2[i] <- "white"
}
}

On Mar 27, 2014, at 15:22, Luis Iván Ortiz Valencia <liov2067@gmail.com> wrote:

> Sorry, change & by | inthe first if.
> 
> Em 28/03/2014 03:13, "Alessandra Carioli" <alessandracarioli@gmail.com> escreveu:
> Thanks for the new code but I still get the error, this time for the  if (varofint[[i]]<m.varofint & varlag[[i]]<m.varlag) sec[i] <- 2 line:
> 
> Error in if (varofint[[i]] < m.varofint & varlag[[i]] < m.varlag) sec[i] <- 2 : missing value where TRUE/FALSE needed
> 
> 
> On Mar 27, 2014, at 13:58, Luis Iván Ortiz Valencia <liov2067@gmail.com> wrote:
> 
>> for (i in 1:n) 
>>   {
>>   if ( !(is.na (varofint[[i]]) & is.na (varlag[[i]] )) )   {
>>     if (varofint[[i]]>=m.varofint & varlag[[i]]>=m.varlag) sec[i] <- 1
>>     if (varofint[[i]]<m.varofint & varlag[[i]]<m.varlag) sec[i] <- 2
>>     if (varofint[[i]]<m.varofint & varlag[[i]]>=m.varlag) sec[i] <- 3
>>     if (varofint[[i]]>=m.varofint & varlag[[i]]<m.varlag) sec[i] <- 4
>>     }
>> }
>> 
>> 
>> 2014-03-27 9:46 GMT-03:00 Alessandra Carioli <alessandracarioli@gmail.com>:
>> it does not work:
>> 
>> > for (i in 1:n) {
>> + if ( ! (  is.na (varofint [[i]]) & is.na (varlag [[i]] ) )
>> + if (varofint[[i]]>=m.varofint & varlag[[i]]>=m.varlag) sec[i] <- 1
>> Error: unexpected 'if' in:
>> "if ( ! (  is.na (varofint [[i]]) & is.na (varlag [[i]] ) )
>> if"
>> > for (i in 1:n) {
>> + if ( ! (  is.na (varofint [[i]]) & is.na (varlag [[i]] ) ) {
>> Error: unexpected '{' in:
>> "for (i in 1:n) {
>> if ( ! (  is.na (varofint [[i]]) & is.na (varlag [[i]] ) ) {"
>> > for (i in 1:n) {
>> +   if ( ! (  is.na (varofint [[i]]) & is.na (varlag [[i]] ) ) 
>> +        {
>> Error: unexpected '{' in:
>> "  if ( ! (  is.na (varofint [[i]]) & is.na (varlag [[i]] ) ) 
>>        {"
>> >     if (varofint[[i]]>=m.varofint & varlag[[i]]>=m.varlag) sec[i] <- 1
>> >     if (varofint[[i]]<m.varofint & varlag[[i]]<m.varlag) sec[i] <- 2
>> Error in if (varofint[[i]] < m.varofint & varlag[[i]] < m.varlag) sec[i] <- 2 : 
>>   missing value where TRUE/FALSE needed
>> >     if (varofint[[i]]<m.varofint & varlag[[i]]>=m.varlag) sec[i] <- 3
>> Error in if (varofint[[i]] < m.varofint & varlag[[i]] >= m.varlag) sec[i] <- 3 : 
>>   missing value where TRUE/FALSE needed
>> >     if (varofint[[i]]>=m.varofint & varlag[[i]]<m.varlag) sec[i] <- 4
>> >   }
>> Error: unexpected '}' in "  }"
>> > 
>> > }
>> Error: unexpected '}' in "}"
>> > 
>> 
>> 
>> 
>> On Mar 27, 2014, at 13:36, Luis Iván Ortiz Valencia <liov2067@gmail.com> wrote:
>> 
>>> 
>>> ok.
>>>> 
>>> sec <- c(1:n)
>>> 
>>> 
>>> 
>>> for (i in 1:n) {
>>>>  if ( ! (  is.na (varofit [[i]]) & is.na (varlag [[i]] ) ) # try this 
>>>> 
>>> 
>>> 
>>> {
>>>  if (varofint[[i]]>=m.varofint & varlag[[i]]>=m.varlag) sec[i] <- 1
>>>  if (varofint[[i]]<m.varofint & varlag[[i]]<m.varlag) sec[i] <- 2
>>>  if (varofint[[i]]<m.varofint & varlag[[i]]>=m.varlag) sec[i] <- 3
>>>  if (varofint[[i]]>=m.varofint & varlag[[i]]<m.varlag) sec[i] <- 4
>>> }
>>> 
>>> }
>>> 
>>> 
>>> 
>>> 2014-03-27 9:21 GMT-03:00 Alessandra Carioli <alessandracarioli@gmail.com>:
>>> could you be more specific?
>>> 
>>> here is a reproducible sample of the variable with NAs
>>> 
>>> varofint<-
>>> c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0, 0, NA, 0, NA, 0, 0, NA, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> NA, 0, 0.164933305, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.291271917, 0, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, NA, 0, NA, 0, 0, NA, 0, 0, 0, 0, NA, NA, 0, 
>>> 0, 0.537484572, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, 0, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.092115934, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, 0, 0, 0, NA, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0.296479971, 0, 0, 0, 0, 0, 0, NA, 0, NA, 0, 0, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, 0, 0, 0, NA, 0, 0, NA, NA, 0, 
>>> 0, 0, 0, NA, NA, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.295789209, 0.303971639, 0.219306412, 
>>> 0, 0, 0.331778037, 0, 0, 0, 0, 0, 0.197967541, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0.295266344, 0.209663895, 0, 
>>> 0, 0.243258498, 0, 0, 0, 0, 0, 0, 0.185960019, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, 0, 0.401177728, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.403838006, 
>>> 0.831365011, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0.20709785, 0.058843094, 0.247471432, 
>>> 0, 0, 0, 0, 0, 0.066125464, 0.304544571, 0.161387936, 0, 0.556304427, 
>>> 0.221202234, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.486641686, 
>>> 0, 0.493005485, 0, 0, 0, 0.293090394, 0.144244431, 0.30881235, 
>>> 0.190732519, 0.22571382, 0.141256239, 0, 0, 0.221200487, 0, 0, 
>>> 0.355637747, 0, 0, 0.109983777, 0, 0, 0.309099901, 0.26444824, 
>>> 0, 0, 0.208762262, 0, 0, 0, 0, 0, 0, 0.386369995, 0, 0, 0, 0, 
>>> 0, 0.610002111, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0, 0, 0.304533746, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.253646164, 
>>> 0, 0, 0, 0, 0, 0.292352545, 0.199674325, 0, 0, 0.077409866, 0, 
>>> 0.178306338, 0, 0.247031484, 0, 0, 0, 0, 0.571081843, 0, 0, 0, 
>>> 0.25148797, 0, 0, 0, 0, 0.525829577, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0.309912782, 0.210730392, 0, 0.415839713, 0.209504522, 0.248390636, 
>>> 0.177825198, 0.15989383, 0.187727558, 0, 0, 0, 0.198002376, 0.369358774, 
>>> 0, 0.189799032, 0.291134941, 0.296698816, 0.338678884, 0.288813669, 
>>> 0.316312962, 0.463492707, 0, 0.19105211, 0.338118033, 0, 0, 0.212219545, 
>>> 0, 0, 0, 0.261451579, 0.458265609, 0.209412123, 0.169391039, 
>>> 0, 0, 0.27338277, 0, 0, 0, 0, 0, 0, 0, 0, 0.39689026, 0, 0, 0.492665826, 
>>> 0, 0.290313684, 0, 0, 0.309593529, 0, 0, 0.392168397, 0, 0, 0, 
>>> 0.390101182, 0, 0, 0, 0.217629563, 0.35176174, 0.163757246, 0.221287896, 
>>> 0, 0, 0, 0, 0.311330729, 0, 0.313226459, 0, 0, 0.336015233, 0.055940549, 
>>> 0.407129286, NA, 0, 0, 0.305481042, 0, 0.194203896, 0.096459921, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, 0, 0.376684098, 0, 0, 0.16270304, 
>>> 0, 0, 0, 0, 0.290927227, 0.219878573, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.245887085, 0.535651744, 
>>> 0.283752341, 0, 0.277970813, 0, 0, 0.088074687, 0, 0, 0, 0, 0, 
>>> 0, 0.202280546, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, NA, 0, 0, 0, NA, 0, 0, NA, NA, 0, 0, 
>>> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
>>> 0)
>>> 
>>> On Mar 27, 2014, at 13:08, Luis Iván Ortiz Valencia <liov2067@gmail.com> wrote:
>>> 
>>>> Well before command for you should filter NA , that is the problem. Maybe another for with condition if(!is.na (varofit [[i]]) & is.na (varlag [[i]])
>>>> 
>>>> Em 27/03/2014 11:59, "Alessandra Carioli" <alessandracarioli@gmail.com> escreveu:
>>>> Yes, there is in varofint and in varlag, but in varlag I take care of it (I think) using NAOK=T. In varofint I am not sure how to do it without changing the length of my variable (na.pass does not help)
>>>> 
>>>> 
>>>> On Mar 27, 2014, at 12:55, Luis Iván Ortiz Valencia <liov2067@gmail.com> wrote:
>>>> 
>>>>> Hi
>>>>> Verify if there is a Na in vector varofint and varlag.
>>>>> 
>>>>> Em 27/03/2014 11:35, "Alessandra Carioli" <alessandracarioli@gmail.com> escreveu:
>>>>> Dear All,
>>>>> 
>>>>> I am doing cluster maps (spatial polygons) for some variables which contain NAs. Everything goes smoothly until I try to derive the 4 sectors High-High/Low—Low. At that point I get this error message:
>>>>> 
>>>>> Error in if (varofint[[i]] >= m.varofint & varlag[[i]] >= m.varlag) sec[i] <- 1 : missing value where TRUE/FALSE needed
>>>>> 
>>>>> I have tried a couple of things to get around the problem but nothing seems to work so far. Any help on the issue is greatly appreciated!
>>>>> 
>>>>> Ale
>>>>> 
>>>>> Here is my code:
>>>>> 
>>>>> library(spdep)
>>>>> listw <- nb2listw(nb) #nb is my neighbor matrix
>>>>> varofint #variable to be plotted
>>>>> varofint.name <- "Princeton Index 1997 Clusters”
>>>>> varlag <-lag.listw(listw, varofint, NAOK=T)
>>>>> # Calculate Mean of Variable of interest and lagged value of variable of interest
>>>>> m.varofint <- mean(varofint,na.rm=T)
>>>>> m.varlag <- mean(varlag,na.rm=T)
>>>>> # Calculate Lisa Test
>>>>> lisa.FOQ <- localmoran(varofint,listw, alternative="two.sided",na.action=na.exclude)
>>>>> # Get significance level
>>>>> vec <- c(1:n)
>>>>> significance <- 0.05
>>>>> vec <- ifelse(lisa.FOQ[,5] < significance, 1,0)
>>>>> plot.only.significant <- TRUE # to plot only significant areas below 0.05
>>>>> # Derive sector HERE IS THE PROBLEM
>>>>> sec <- c(1:n)
>>>>> for (i in 1:n) {
>>>>>  if (varofint[[i]]>=m.varofint & varlag[[i]]>=m.varlag) sec[i] <- 1
>>>>>  if (varofint[[i]]<m.varofint & varlag[[i]]<m.varlag) sec[i] <- 2
>>>>>  if (varofint[[i]]<m.varofint & varlag[[i]]>=m.varlag) sec[i] <- 3
>>>>>  if (varofint[[i]]>=m.varofint & varlag[[i]]<m.varlag) sec[i] <- 4
>>>>> }
>>>>> 
>>>>> # Define colors for sectors: this works fine
>>>>> sec.all <- sec
>>>>> colors1 <- c(1:n)
>>>>> for (i in 1:n) {
>>>>>  if (sec.all[i]==1) colors1[i] <- “red"
>>>>>  if (sec.all[i]==2) colors1[i] <- “blue"
>>>>>  if (sec.all[i]==3) colors1[i] <- “lightblue"
>>>>>  if (sec.all[i]==4) colors1[i] <- “pink"
>>>>>  if (sec.all[i]==0) colors1[i] <- “white"
>>>>> }
>>>>> loc.m.data <- sec*vec
>>>>> colors2 <- colors1
>>>>> for (i in 1:n) {
>>>>>  if (loc.m.data[i]==0) colors2[i] <- “white” # ERROR MESSAGE COMES AGAIN
>>>>> }
>>>>> 
>>>>> _______________________________________________
>>>>> R-sig-Geo mailing list
>>>>> R-sig-Geo@r-project.org
>>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>> 
>>> 
>>> 
>>> 
>>> 
>>> -- 
>>> Luis Iván Ortiz Valencia
>>> Doutorando Saúde Pública - Epidemiologia, IESC, UFRJ
>>> Estatístico Msc.
>>> Spatial Analyst Msc.
>>> 
>> 
>> 
>> 
>> 
>> -- 
>> Luis Iván Ortiz Valencia
>> Doutorando Saúde Pública - Epidemiologia, IESC, UFRJ
>> Estatístico Msc.
>> Spatial Analyst Msc.
>> 
> 


	[[alternative HTML version deleted]]

