0

I am working with an S4 object (I believe) that I need to change. It looks something like this:

str(dataK@K)  num [1:226, 1:226] 0.94828 -0.13679 -0.01083 -0.12882
-0.00399 ...
 - attr(*, "dimnames")=List of 2   ..$ : chr [1:226] "GT_1" "GT_10" "GT_100" "GT_101" ...   ..$ : chr [1:226] "GT_1" "GT_10" "GT_100" "GT_101" ...
>

Now, I need to replace the num-part with an identity matrix and keep the attributes (the chr[1:226] part) as it is. I am totally unfamiliar with S4 slots, and it may be a trivial solution - which I do not know. So I'd be much obliged. Thanks jahn BTW: i've tried dataK@K <- as.matrix(diag(226)) but that takes away the attributes.

SigneMaten
  • 493
  • 1
  • 6
  • 13
  • 1
    The attribute is not part of the slot, it is part of the matrix stored in the slot. You probably need to do `dataK@K <- matrix(diag(226), dimnames = dimnames(dataK@K))`. I can't test without minimal reproducible example. – Roland Mar 05 '21 at 08:03
  • 1
    if you use empty brackets, you can simpliy further: dataK@K[] <- diag(226) – Sirius Mar 05 '21 at 08:21

0 Answers0