[R] Extracting diagonal matrix

A Ezhil ezhil02 at yahoo.com
Fri Nov 21 18:05:39 CET 2008


Hi,

Thank you very much for the help. c[lower.tri(c,diag=FALSE)] works fine for me. 

Thanks again.

Kind regards,
Ezhil

--- On Fri, 11/21/08, Anup Menon <anup.nandialath at gmail.com> wrote:

> From: Anup Menon <anup.nandialath at gmail.com>
> Subject: Re: [R] Extracting diagonal matrix
> To: ezhil02 at yahoo.com
> Cc: r-help at r-project.org
> Date: Friday, November 21, 2008, 9:16 PM
> Hi Ezhil,
> 
> Maybe this will help. There might be an easier way to do
> this but here is
> one solution.
> 
> tril <- function(A)
> {
>  A <- as.matrix(A)
>  cmats <- matrix(rep(1,length(A)),dim(A)[1],dim(A)[2])
>  upper.tri(cmats,diag=T)
>  cmats[upper.tri(cmats)] <- 0
>  out <- A*cmats
>  return(out)
> }
> 
> tril(correlation.matrix)
> 
> will give you the output.
> 
> HTH
> 
> Best Regards
> 
> Anup
> 
> On Fri, Nov 21, 2008 at 7:28 AM, A Ezhil
> <ezhil02 at yahoo.com> wrote:
> 
> > Dear All,
> >
> > I have a correlation matrix of size 100 x 100 and
> would like to extract the
> > diagonal matrix from it. I have used the for loop to
> store tha correlation
> > values of the diagonal matrix. Is there a 'R
> way' of doing this?
> >
> > Thanks in advance.
> >
> > Kind regards,
> > Ezhil
> >
> > ______________________________________________
> > 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.
> >



More information about the R-help mailing list