Hi,
In the following function, i hope to save my simulated data into the
"result" dataset, but why the final "result" dataset seems
not to be
generated.
#Function
simdata<-function (nsim) {
result<-matrix(NA,nrow=nsim,ncol=2)
colnames(result)<-c("x","y")
for (i in 1:nsim) {
set.seed(i)
result[i,]<- cbind(runif(1),runif(1))
}
return(result)
}
#simulation> simdata(10) #correct result
x y
[1,] 0.2655087 0.372123900
[2,] 0.1848823 0.702374036
[3,] 0.1680415 0.807516399
[4,] 0.5858003 0.008945796
[5,] 0.2002145 0.685218596
[6,] 0.6062683 0.937641973
[7,] 0.9889093 0.397745453
[8,] 0.4662952 0.207823317
[9,] 0.2216014 0.024233910
[10,] 0.5074782 0.306768506
But, the dataset "result" wasnot assigned the above values. What is
the
problem?> result #wrong result??
x y
[1,] NA NA
[2,] NA NA
[3,] NA NA
[4,] NA NA
[5,] NA NA
[6,] NA NA
[7,] NA NA
[8,] NA NA
[9,] NA NA
[10,] NA NA>
Thanks a lot.
--
-----------------
Jane Chang
Queen's
[[alternative HTML version deleted]]
David Winsemius
2009-Dec-09 04:22 UTC
[R] Why cannot get the expected values in my function
On Dec 8, 2009, at 11:07 PM, rusers.sh wrote:> Hi, > In the following function, i hope to save my simulated data into the > "result" dataset, but why the final "result" dataset seems not to be > generated.> #Function > simdata<-function (nsim) {# Instead why not: cbind(x=runif(nsim), y=runif(nsim) )> } > > #simulation >> simdata(10) #correct result > x y > [1,] 0.2655087 0.372123900 > [2,] 0.1848823 0.702374036 > [3,] 0.1680415 0.807516399 > [4,] 0.5858003 0.008945796 > [5,] 0.2002145 0.685218596 > [6,] 0.6062683 0.937641973 > [7,] 0.9889093 0.397745453 > [8,] 0.4662952 0.207823317 > [9,] 0.2216014 0.024233910 > [10,] 0.5074782 0.306768506 > But, the dataset "result" wasnot assigned the above values. What is > the > problem? >> result #wrong result?? > x y > [1,] NA NA > [2,] NA NA > [3,] NA NA > [4,] NA NA > [5,] NA NA > [6,] NA NA > [7,] NA NA > [8,] NA NA > [9,] NA NA > [10,] NA NA >> > Thanks a lot. > -- > ----------------- > Jane Chang > Queen's > > [[alternative HTML version deleted]] > > ______________________________________________ > 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, MD Heritage Laboratories West Hartford, CT