# [R] Expanding a VCV Matrix

Maithula Chandrashekhar m.chandrashekhar1977 at gmail.com
Thu Apr 21 17:17:47 CEST 2011

```Oh.........you are right.........sorry. So many work nowadays, even
cant see things properly.

2011/4/21 Uwe Ligges <ligges at statistik.tu-dortmund.de>:
>
>
> On 21.04.2011 17:08, Maithula Chandrashekhar wrote:
>>
>> Thanks Uwe for your insight. However this could not solve my problem.
>> Actually, I meant to define 'Str' in this way:
>>
>>> mat<- matrix(c(1,2,0,2,5,0.5,0,0.5,3), 3, 3)
>>> colnames(mat)<- rownames(mat)<- paste("variable", 1:3, sep="")
>>> mat
>>
>>           variable1 variable2 variable3
>> variable1         1       2.0       0.0
>> variable2         2       5.0       0.5
>> variable3         0       0.5       3.0
>>>
>>> Str<- c(paste("variable", 1:4, sep=""), "variable2"); Str
>>
>> [1] "variable1" "variable2" "variable3" "variable4" "variable2"
>>
>> Therefore 'variable2' should appear twice in my modified VCV matrix,
>> which was not there in your suggestion. Is there any better any on how
>> can I do that? or it is really imposable to achieve?
>
> It is there twice if the strings and the rownames match.
>
> Uwe
>
>
>>
>> Thanks,
>>
>> 2011/4/21 Uwe Ligges<ligges at statistik.tu-dortmund.de>:
>>>
>>>
>>> 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?
>>>>
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help