Re: [R] Expanding a VCV Matrix

From: Uwe Ligges <ligges_at_statistik.tu-dortmund.de>
Date: Thu, 21 Apr 2011 14:14:13 +0200

On 20.04.2011 19:26, Maithula Chandrashekhar wrote:
> Dear all, I have special task to expand a given VCV matrix, however
> could not accomplice yet. Let say I have following VCV matrix
>
>> mat<- matrix(c(1,2,0,2,5,0.5,0,0.5,3), 3, 3)
>> colnames(mat)<- rownames(mat)<- paste("variable", 1:3)
>> mat
> variable 1 variable 2 variable 3
> variable 1 1 2.0 0.0
> variable 2 2 5.0 0.5
> variable 3 0 0.5 3.0
>
> Now, say I have a general string vector like this:
>
>> Str<- c(paste("variable", 1:4), "variable2")
>> Str
> [1] "variable 1" "variable 2" "variable 3" "variable 4" "variable 2"

If Str is like the printed one above rather than the code example, you can do:

  mat[Str[Str %in% rownames(mat)], Str[Str %in% colnames(mat)]]

Uwe Ligges

> Now according to this string, I want my previous VCV matrix also
> expands. Therefore, as "variable 4" is not there in VCV matrix, so it
> will be ignored. Therefore final VCV matrix will be of order 4, and
> will look like:
>
> variable 1 variable 2 variable 3 variable 2
> variable 1 1 2 0 2
> variable 2 2 5 0.5 5
> variable 3 0 0.5 3 0.5
> variable 2 2 5 0.5 5
>
> However, I do not think it is just some straightforward expansion,
> which could be done just by the subsetting mechanism of R. Is there
> any idea on how can I do it for general case?
>
> Thanks for your time,
>
> ______________________________________________
> 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.



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. Received on Thu 21 Apr 2011 - 12:22:08 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 21 Apr 2011 - 15:20:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive