"Achaz von Hardenberg" <fauna at pngp.it> writes:
> hi,
> sorry for a possibly naive question but I am a bit of a beginner in R
programming...
>
> I wrote a function which simulates some data and performs two
> different kinds of analyses on it. as an output I get the statistics
> for the two analyses (t-values). Now I would like to have an other
> function which reruns my first function say a 1000 times and
> attaches the resulting statistics in a data.frame so that at the end
> it contains 1000 rows with two columns for the two statistics I
> calculated with my first function.
>
> I hope I was clear enugh and would be glad to anybody who can help me out!
replicate is a good start:
>
replicate(10,t.test(rexp(10),mu=1)[c("statistic","p.value")])
[,1] [,2] [,3] [,4] [,5] [,6]
statistic -1.552478 -1.09727 -2.053807 -1.671855 -0.1169578 0.1005037
p.value 0.1549645 0.3010120 0.07018008 0.1288855 0.9094619 0.9221477
[,7] [,8] [,9] [,10]
statistic -0.1711356 -0.933484 0.3169710 -1.136498
p.value 0.867903 0.3749356 0.758495 0.2851029
As you see, the result is a 2xn matrix. If you really need a data
frame, just use as.data.frame(t(....)), or (bypassing the matrix
entirely):
> do.call("rbind",replicate(10,as.data.frame(
t.test(rexp(10),mu=1)[c("statistic","p.value")]),
simplify=FALSE))
statistic p.value
t 0.32566430 0.7521223
t1 -1.22741479 0.2508023
t2 -1.66792987 0.1296757
t3 1.56440274 0.1521619
t4 0.63778111 0.5395015
t5 -1.03826715 0.3262346
t6 0.09337127 0.9276542
t7 0.90166085 0.3907282
t8 -0.78164107 0.4544958
t9 -0.39766367 0.7001452
--
O__ ---- Peter Dalgaard ??ster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907