[R] looping

Neil Hepburn nhepburn at ualberta.ca
Tue Feb 27 01:11:08 CET 2007


Greetings:

I am looking for some help (probably really basic) with looping. What I want
to do is repeatedly sample observations (about 100 per sample) from a large
dataset (100,000 observations).  I would like the samples labelled sample.1,
sample.2, and so on (or some other suitably simple naming scheme).  To do
this manually I would 

>smp.1 <- sample(100000, 100)
>sample.1 <- dataset[smp.1,]
>smp.2 <- sample(100000, 100)
>sample.2 <- dataset[smp.2,]
.
.
.
>smp.50 <- sample(100000, 100)
>sample.50 <- dataset[smp.50,]

and so on.

I tried the following loop code to generate 100 samples:

>for (i in 1:50){
>+ smp.[i] <- sample(100000, 100)
>+ sample.[i] <- dataset[smp.[i],]}

Unfortunately, that does not work -- specifying the looping variable i in
the way that I have does not work since R uses that to reference places in a
vector (x[i] would be the ith element in the vector x)

Is it possible to assign the value of the looping variable in a name within
the loop structure?

Cheers,
Neil Hepburn

===========================================
Neil Hepburn, Economics Instructor
Social Sciences Department,
The University of Alberta Augustana Campus
4901 - 46 Avenue 
Camrose, Alberta
T4V 2R3

Phone (780) 697-1588
email nhepburn at augustana.ca



More information about the R-help mailing list