[R] Use of the command 'replicate'

Peter Ehlers ehlers at ucalgary.ca
Thu Oct 22 14:26:06 CEST 2009


See if this gives you a hint:

  size <- 50
  num  <- 100
  sum(size[1:(num/2)])

  -Peter Ehlers

Manny Gomez wrote:
> Dear R users,
> 
> I'd like to ask u whether you know how to sort out the following:
> 
> I'm trying to reproduce a dataset of clusters, and for that I need to build
> up a cluster index (inside a function) using the command "replicate" as
> follows:
> 
> 
> dataset<- function(
> clusters=100,
> cluster.size=50,
> outcome.mean=list(mean=3, sd=1),
> outcome.var=list(mean=0, sd=1),
> treat=1,
> unbal = FALSE){
> 
> if(clusters%%2!=0) stop("number of clusters, clusters, must be an even
> number \n")
> 
> clust.size<-cluster.size
> clusters<-clusters
> 
> clust.index <- rep(1:clusters, times=clust.size)               # cluster
> index
> treat.ind <- c(                                                            
> # treatment index indicator
>     rep(0, sum(clust.size[1:(clusters/2)])),
>     rep(1, sum(clust.size[(clusters/2+1):clusters]))
>     )
> 
> (...)
> 
> }
> 
> Doing this it gives me an error saying that the 'times' argument is invalid
> and that the cluster and treatment indicator indices are both invalid, and
> therefore, I'm probably defining "clust.size" wrongly?!
> Could you elucidate me about this??
> 
> Thanks a lot.
> 
> Manny Gomez




More information about the R-help mailing list