[R] dimnames in array
David Winsemius
dwinsemius at comcast.net
Wed Aug 8 16:52:13 CEST 2012
On Aug 8, 2012, at 3:19 AM, aleksandr russell wrote:
> Hello,
>
> I'm working with an array; I'm trying to make it so that an array of
> dim(42,2,2) has names whose length corresponds to that of the array,
> and am hoping someone with experience with this can see what I'm not
> doing correctly:
>
>
>
>
> data11 = array(0,c(41,2,2))
>
> y = lsoda(x0,times,fhn$fn.ode,pars)#This is make.fhn() from colloc
> infer package#
> y = y[,2:3]
>
> data11<-array(0,c(41,2,2))
>
>
> m8<-cbind(.29,1:41)
> m9<-as.array(m8,dim=c(41,2))
>
> data11[,1,] = y+m9
> data11[,2,] = y+m9
I didn't run that code since your difficulties did not seem to have
anything to do with the values inside the array but only with the
dimension labeling.
>
> rownames(data2, do.NULL = FALSE, prefix="row")
What is 'data2'?
> varnames=c("V","R")
> colnames(data6)=c("one","two")
Ditto... what is 'data6'?
> colnames(data10)=colnames(data6)
> dimnames(data11)=list(rownames(data2),varnames,colnames(data10))
> data11a<-as.array(data11,dimnames=dimnames(data11))
> attributes(data11a)
>
> #so this seems to print out the array attributes as they
> should be:
For which object?
> two sets of two columns each, and the first column of
> each set is called 'v' and the second 'r'- no problem#
The data11 object had three dimenstions.
>
> now the analysis from colloc infer(I've put it in below*)
> takes this array but will not complete the analysis, saying
>
> "Error in `colnames<-`(`*tmp*`, value = c("V", "R")) :
> length of 'dimnames' [2] not equal to array extent"
Here's one way to label a 41 x 2 x 2 array:
> dimnames(data11)<- c(list(X=paste0("X",1:41)), list(Y=paste0("Y",
1:2)), list(Z=paste0("Z",1:2)) )
> attributes(data11)
$dim
[1] 41 2 2
$dimnames
$dimnames$X
[1] "X1" "X2" "X3" "X4" "X5" "X6" "X7" "X8" "X9" "X10"
"X11" "X12" "X13" "X14" "X15" "X16" "X17" "X18" "X19"
[20] "X20" "X21" "X22" "X23" "X24" "X25" "X26" "X27" "X28" "X29" "X30"
"X31" "X32" "X33" "X34" "X35" "X36" "X37" "X38"
[39] "X39" "X40" "X41"
$dimnames$Y
[1] "Y1" "Y2"
$dimnames$Z
[1] "Z1" "Z2"
>
> When I type in
>
> length(dimnames(data11a)[2])
>
> I get the answer:
>
> [1] 1
>
> which seems odd when
>
> dimnames(data11a)[2]
>
> gives
>
> [[1]]
> [1] "V" "R"
It is a one element list whose single element is a 2 element vector.
>
> I would like to ask "How can I get the names of the second dimension
> of the array(data11a) to have length "2" ?
>
> It seems to me that the analysis will run if I can make this array
> have the correct length dimnames
>
> I thank you for your help.
>
> ____________
>
>
> *
> fd.data11a = data11a
> DEfd11a = Data2fd(fd.data11a, times, bbasis,
> fdnames=list(NULL,NULL,varnames))
>
>
> profile11a.obj=LS.setup(pars,DEfd11a
> $coefs,fhn,basisvals=bbasis,lambda,fd.obj=NULL,
>
> more
> =NULL,data=data11a,weights=NULL,times=times,quadrature=NULL,eps=1e-6,
> posproc=FALSE,poslik=FALSE,discrete=FALSE,names=NULL,sparse=FALSE)
> lik = profile11a.obj$lik
> proc = profile11a.obj$proc
> qwts = rep(1/length(knots),length(knots))
> qwts = qwts %*% t(lambda)
> weights = array(1,dim(data11a))
> likmore = make.id()
> likmore$weights = weights
> lik = make.SSElik()
> lik$more = likmore
> lik$bvals = eval.basis(times,bbasis)
> procmore = make.fhn()
> procmore$weights = qwts
> procmore$qpts = qpts
> procmore$names = varnames
> procmore$parnames = parnames
> proc = make.SSEproc()
> proc$more = procmore
> proc$bvals = list(bvals = eval.basis(procmore$qpts,bbasis,0),
> dbvals = eval.basis(procmore$qpts,bbasis,1))
> res11a = inneropt(coefs, times=times, data=data11a, lik=lik,
> proc=proc,
> pars=spars, in.meth='nlminb', control.in=control.out)
--
David Winsemius, MD
Alameda, CA, USA
More information about the R-help
mailing list