[R] Multiple if statement in loop condition

David Winsemius dwinsemius at comcast.net
Wed Nov 20 17:21:44 CET 2013


On Nov 20, 2013, at 4:24 AM, Carl Witthoft wrote:

> What this message means is that a "{" showed up when some other bracket was
> unpaired.  In this case, if you check your code, you'll see that 
> "if(MatriceDist[i,j] > 0 & ((vectorID[i] > 0 | vectorID[j] > 0))"  is
> lacking a closing ")"  for the if clause.

Arguably it is due to the superfluous "(" after the "&". Further evidence perhaps of the value of adding spaces before and after parentheses. Also evidence of the value of an R-aware editor.

-- 
David.
> 
> Phalaen wrote
>> Hi! 
>> I am a Phd student in the university of Padua and I am trying to write a
>> little script (the first!) to make a simulation on epidemiology.
>> I'd like to change the values of a vector of 883 values basing on a
>> neighbour matrix constructed with nb2mat from a shapefile: if i and j (two
>> cells) are neighbour (matrix) and i or j have a positive value in the
>> vector, I'd like to transform the value of both i and j to 1 (positive),
>> otherwise the value of i and j should remain 0. When I launch the next
>> little script:
>> 
>> for(i in 1:883)
>> { for(j in 1:883)
>>  { if(MatriceDist[i,j] > 0 & ((vectorID[i] > 0 | vectorID[j] > 0)) {
>> 	 vectorID[i] = 1 & vectorID[j] = 1
>> 		print(vectorID)
>> } } } 
>> 
>> the answer from the software is:
>> Error: unexpected '{' in:
>> " { for(j in 1:883)
>> { while(MatriceDist[i,j] > 0 & ((vectorID[i] > 0 | vectorID[j] > 0)) {"
>> 
>> I think that it is an error in the statement for if but I can not
>> understand how to solve it...
>> Thank you everyone!
>> Elisa
> 
> 
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Multiple-if-statement-in-loop-condition-tp4680797p4680803.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list