Dear Stephanie, At first I just tried typing and ended up with a butcchered frankenstein of a solution. Then I thought about (novel, right? ;) and realized several of the steps in your for loop could be simplified substantially, namely, the last step is just: exp(- (0:(i - k)))
Then I just pad the start of each vector with NAs, use sapply() which will automatically simplify to a matrix, rather than using an array, use a list to store the different sized matrices. The result is: tim <- c(10,12,5) #number of observations per individual struc <- lapply(tim, function(i) { sapply(1:i, function(k) { c(rep(NA, k - 1), exp(- (0:(i - k)))) }) }) three different sized matrices in a list. If you need the full correlation matrix, not just the lower triangle, you can look at ?t and ?lower.tri Hope this helps, Josh On Fri, Aug 5, 2011 at 2:42 PM, Kliethermes, Stephanie A <stephanie-kliether...@uiowa.edu> wrote: > Dear all, > > > > I am having trouble creating a "jagged array" in R. I have found no > commentary on how to do it in the help files, but maybe I am misunderstanding > the purpose of some of the array functions (e.g. tapply). > > > > I am using a longitudinal dataset where each individual has a different > number of time points (observations). I need to create correlation > structures for each individual which are necessarily different lengths. > Currently I am using for loops in an attempt to do so. The closest I can get > is with the following code (n=3) where I create matrices of the same > dimensions with NA's for timepoints not used. Perhaps there is simply a way > to drop these NA's? > > > > I would appreciate any thoughts on how to create a jagged array to store > matrices of different dimensions or direction towards a help-file or package > that will help me out. > > > > Thanks in advance! > > > > > > n<-3 #sample size > tim<-c(10,12,5) #number of observations per individual > tim.pts<-12 #number of possible observations > struc<-array(NA,dim=c(tim.pts,tim.pts,n)) > > > for(i in 1:n){ > > for(k in 1:tim[i]){ > for(j in k:tim[i]){ > struc[k,j,i]<- (exp(-(j-k))) > struc[j,k,i]=struc[k,j,i] > } > } > > > } > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. > -- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, ATS Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/ ______________________________________________ R-help@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.