[R] Loop and store results

Sam Sam_Smith at me.com
Fri Jan 21 11:47:10 CET 2011


Dear List,

I have a data-frame 

#prepare the data
example <- data.frame(letters[1:9],
       sample(letters, 9),
       sample(letters, 9),
       sample(letters, 9),
       sample(letters, 9),
       sample(letters, 9),
       sample(letters, 9),
       sample(letters, 9),
       sample(letters, 9))
colnames(example) <- c("individuals", 1:8)

I want to sample this 

#sample the data
a_1 <- example[sample(nrow(example),3),]

individuals 1 2 3 4 5 6 7 8
8           h w m r a n v v b
6           f e b g u v r b p
3           c z c s k t e i g

However i want to sample it 500 times, so i need to use the loop function - which is something, unfortunately i am unsure how to write.

Furthermore, i want to output the results in a data-frame ( i think i need the list function, but again i am unsure)

Ideally it would be separated by sample but i am unsure if this is possible? However as long as the order is kept intact that will be fine. I.E the top 3 are sample 1, the next 3 are sample 2 etc

What i require:-

individuals 1 2 3 4 5 6 7 8

8          	     h w m r a n v v b
6                   f e b g u v r b p
3                   c z c s k t e i g
		
9                   h w m f a n v v b
4                   f e b g b v r b p
2                   c z c s k t e i g

If its not too much to ask: I will then sample it 4 individuals 500 times , 5 individuals etc etc and store these _ i can always do these separately if its asking too much!

Thanks,

Sam



More information about the R-help mailing list