Re: [R] Repeating functions

From: Sean Davis <sdavis2_at_mail.nih.gov>
Date: Fri 30 Dec 2005 - 03:09:13 EST

On 12/29/05 11:01 AM, "Kort, Eric" <Eric.Kort@vai.org> wrote:

> Ronnie Babigumira said...

>>
>> Hi, I have a number of spatial weight files and using Roger Bivand's spdep, I
>> would like to
>>
>> 1. Convert them into neighbor lists using
>> 2. Convert the neighbor lists into spatial weights
>>
>> For a given file, the syntax would be
>>
>> mygal_nb1 <- read.gal("mygalfile1", override.id = TRUE)
>> myweight1 <- nb2listw(mygal_nb1)
>>
>> I have mygalfile[i] with i from 1 through to 6 and would like to repeat the
>> above two lines through 1 to 6.
>> something like (the syntax below is not correct...just an illustration)
>>
>> for (i in 1:6) {
>> mygal_nb[i] <- read.gal("mygalfile[i]", override.id = TRUE)
>> myweight[i] <- nb2listw(mygal_nb[i]
>> }
>>
>>
>> Kindly point me in the right direction (whilst read through the material I
>> have)
> 
> Looks like you want to create lists of objects.  Likely the best place to
> start is looking at the section on lists in An Introduction to R (which came
> with your R distribution and can be accessed via help.start(), or you can
> download it here: http://cran.r-project.org/doc/manuals/R-intro.pdf).

To be just a bit more explicit, see (untested) code. Lists are quite powerful data structures, so learn to use them well.

 mygal_nb <- list()
 myweight <- list()
 for (i in 1:6) {
 mygal_nb[[i]] <- read.gal(mygalfile[i], override.id = TRUE)  myweight[[i]] <- nb2listw(mygal_nb[[i]])  }

Sean



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Dec 30 03:14:32 2005

This archive was generated by hypermail 2.1.8 : Fri 30 Dec 2005 - 04:40:01 EST