[R] Generating Data mvrnorm and loops

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.ac.be
Thu Jan 6 17:06:52 CET 2005


Hi Harold,

check the following:

library(MASS)
Sigma <- 
matrix(c(400,80,80,80,80,400,80,80,80,80,400,80,80,80,80,400), 4, 4)
mu <- c(100,150,200,250)
############

# generating N datasets and store in a list
lis <- lapply(1:10, mvrnorm, n=10, mu=mu, Sigma=Sigma)

# `lis1' contains the extra column whic is the sum of the first two
lis1 <- lapply(lis, function(x) cbind(x, x[,1]+x[,2]))

# `lis2' is `lis1' in a long format with the extra id indicator
lis2 <- lapply(lis, function(x) data.frame(id=rep(1:nrow(x), 
each=ncol(x)), score=c(t(x))))

#######
lis[[1]]; lis2[[1]]

I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Doran, Harold" <HDoran at air.org>
To: <r-help at stat.math.ethz.ch>
Sent: Thursday, January 06, 2005 4:10 PM
Subject: [R] Generating Data mvrnorm and loops


> Dear List:
>
> I am generating N datasets using the following
>
> Sigma<-matrix(c(400,80,80,80,80,400,80,80,80,80,400,80,80,80,80,400),4,4
> )
> mu<-c(100,150,200,250)
>
> N=100
> for(i in 1:N)
> {
> assign(paste("Data.", i, sep=''),
> as.data.frame(cbind(seq(1:1000),(mvrnorm(n=1000, mu, Sigma)))))
> }
>
> With these datasets, I need to work on some of the variables and 
> then
> run each dataset through a linear model. I am having some trouble
> working with variables within the loop and wonder if anyone can 
> offer
> any pointers.
>
> The first thing I am trying to do is add 2 variables together that 
> are
> in each dataset. I am sure this is extremely trivial, but I can't 
> seem
> to get that to work.
>
> I have tried:
>
> for (i in 1:5){
> assign(paste("x",i,sep=""),(get(paste("Data.",i,sep=""))[["V2"]])+(get(p
> aste("Data.",i,sep=""))[["V2"]]))
> }
>
> Now, this code works, but I want for this vector to be a variable 
> within
> each dataframe. Outside the loop, the equivalent code would be
>
> attach(Data.1)
> Data.1$V6<-V1+V2
> Detach(Data.1)
>
> Another task I would like to perform is to reshape each dataframe 
> for
> longitudinal analysis. I have tried the following:
>
> for (i in 1:5){
> assign(paste("long",i,sep=""),reshape(paste("Data.",i,sep=""),idvar=get(
> paste("Data.",i,sep="")[["V1"]]),
> varying=list(names(get(paste("Data",i,sep="")[["V2"]]):get(paste("Data",
> i,sep="")[["V5"]])),v.names="score",direction="long")
> }
>
> This isn't working and I'm not sure if the code is even close.
>
> In general, performing all of these operations outside a loop for a
> single dataframe is simple. My trouble is performing equivalent
> operations within a loop.
>
> Thanks for any help offered.
>
> Harold
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list