Another way:
> A <- matrix(1:9,3,3,
dimnames=list(Row=paste0("r",1:3),Col=paste0("c",1:3)))
> A
Col
Row c1 c2 c3
r1 1 4 7
r2 2 5 8
r3 3 6 9
> matrix( A[row(A)!=col(A)], nrow(A)-1, ncol(A), dimnames=list(NULL,
colnames(A)))
c1 c2 c3
[1,] 2 4 7
[2,] 3 6 8
Bill Dunlap
An even better solution because it has fewer steps.
A <- matrix(1:9, 3, 3)
A
B <- A[-1, ]
B[upper.tri(B, diag=FALSE)] <- A[upper.tri(A)]
B
> A <- matrix(1:9, 3, 3)
> A
[,1] [,2] [,3]
[1,]147
[2,]258
[3,]369
> B <- A[-1, ]
> B[upper.tri(B, diag=FALSE)] <- A
Steve's method is very slick.
I think this is a bit easier to understand.
A <- matrix(1:9, 3, 3)
A
B <- matrix(nrow=2, ncol=3)
B[lower.tri(B, diag=TRUE)] <- A[lower.tri(A)]
B[upper.tri(B, diag=FALSE)] <- A[upper.tri(A)]
B
> A <- matrix(1:9, 3, 3)
> A
[,1] [,2] [,3]
[1,]147
[2,]
> With your code you just remove diagonal elements from your matrix.
Worse; it removed _all_ elements from the matrix that match _anything_ in the
diagonal!
Which, in that example, was everything ...
***
This email and any attach
i) Your code creates w2 but references w1 to create aa.
So you needed
aa <- matrix(rep(c(0.4, 0.1, 0.2), 3), 3,3)
for a working example.
ii) This
> matrix(as.numeric(aa)[!as.numeric(aa) %in% diag(aa)],2,3)
removes any value that is present in the diagonal of aa. Look up ?"%in%" to see
what that
Hi
Your mail is mess due to HTML formating. Please use plain taxt mail.
You got an advice, did you try it?
With your code you just remove diagonal elements from your matrix. If this is
not your intention, you should specify more clearly what do you want to achieve
as the result.
Cheers
Petr
>
When that arrived it was a complete mess since you posted in HTML which
scrambles your code and you sent code which had syntax errors. Please
try again by posting in plain text and cut and paste your code. It would
also help if you stated exactly what you expected your output to consist of.
Mi
Hi.
You did not specify what do you want to do with the result.
functions
upper.tri, lower.tri and diag can manipulate parts of matrices.
Cheers
Petr
> -Original Message-
> From: R-help On Behalf Of malika yassa via R-
> help
> Sent: Wednesday, November 14, 2018 12:20 PM
> To: R-help
8 matches
Mail list logo