# [R] replicate and as.matrix: different behaviour between batch and non-batch mode

From: Oliver Bandel <oliver_at_first.in-berlin.de>
Date: Thu, 06 Nov 2008 14:36:44 +0100

Hello,

sampmeanvec <- function (from, n, repititions)
{

print( paste("samplesize n:", n, "repititions:", repititions) )   samples.mat <- as.matrix( replicate( repititions, sample(from, n) ) )
```  # would that case-check be necessary?
#if( n == 1 )
#{
#  samples.mat <- t (samples.mat)
#}

```

print( "Dim of matrix:")
print( dim(samples.mat) )

meanvec <- apply(samples.mat, 2, mean)   return(meanvec)
}

gleichsamp <- runif(10000)

for( sampsize in c(1,2,4,8,16,32,64,128) )
{

sampmeanvec(gleichsamp, sampsize, 20)
}

The following result:

> source("central_limit_theorem.R")
``` "samplesize n: 1 repititions: 20"
 "Dim of matrix:"
 20  1
 "samplesize n: 2 repititions: 20"
 "Dim of matrix:"
  2 20
 "samplesize n: 4 repititions: 20"
 "Dim of matrix:"
  4 20
 "samplesize n: 8 repititions: 20"
 "Dim of matrix:"
  8 20
 "samplesize n: 16 repititions: 20"
 "Dim of matrix:"
 16 20
 "samplesize n: 32 repititions: 20"
 "Dim of matrix:"
 32 20
 "samplesize n: 64 repititions: 20"
 "Dim of matrix:"
 64 20
 "samplesize n: 128 repititions: 20"
 "Dim of matrix:"
 128  20
```

>

Look at the first dimension: there the cols and rows are changed.

I tried directly in the R-shell:

> x <- 1:20
> dim( as.matrix( replicate(1, sample(x, length(x)) ) ))  20 1
> dim( as.matrix( replicate(2, sample(x, length(x)) ) ))  20 2
> dim( as.matrix( replicate(3, sample(x, length(x)) ) ))  20 3
> dim( as.matrix( replicate(4, sample(x, length(x)) ) ))  20 4

This looks good (and correct to me).

Why is the cols and rows dimensions be changed?

Typed in directly (see above), it works as expected.

Ciao,

Oliver

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. Received on Thu 06 Nov 2008 - 13:41:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 06 Nov 2008 - 16:30:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.