how to define elements in an array according to their order?

Hello!

I intend to do the following. Given matrix A:

A <- matrix(c(2,3,1,3,NA,7,1,NA,NA), nrow=3, ncol=3)
A

I want to order the elements of each column, and then write the position of the row, plus one character, in descending order according to the initial value. That is, I want to transform matrix A to matrix B (where the character is "b"):

B <- matrix(c("b2", "b1", "b3", "b3", "b1", NA, "b1", NA, NA), nrow=3, ncol=3)
B

you know how I can automate this? go from matrix A to matrix B?

Your help is very useful to me.
Best,

So what you want is apply order() to each column. There's a small problem, which is that order() will put the NA at the end, but then still give their order (while you also want their order itself to be NA). So here I wrote order_pad() that adds back the NAs after ordering.

Then we need to paste() the other character. I use B_char[] to make clear that I want want to replace the content of B_char, keeping the matrix structure.

And since paste coerces NA to "NA", I replaced them back.

A <- matrix(c(2,3,1,3,NA,7,1,NA,NA), nrow=3, ncol=3)
B <- matrix(c("b2", "b1", "b3", "b3", "b1", NA, "b1", NA, NA),
            nrow=3, ncol=3)



order_pad <- function(x, ...){
  y <- order(x, ...)
  
  n <- length(y)
  k <- sum(is.na(x))
  
  if(k>0) y[(n-k+1):n] <- NA
  y
}

# Find the order of each column
B_order <- apply(A, 2, order_pad, decreasing = TRUE)

# replace each element by that element with the character
B_char <- B_order
B_char[] <- paste0("b", B_char)

B_char[B_char == "bNA"] <- NA_character_

B_char
#>      [,1] [,2] [,3]
#> [1,] "b2" "b3" "b1"
#> [2,] "b1" "b1" NA  
#> [3,] "b3" NA   NA

B
#>      [,1] [,2] [,3]
#> [1,] "b2" "b3" "b1"
#> [2,] "b1" "b1" NA  
#> [3,] "b3" NA   NA
all.equal(B, B_char)
#> [1] TRUE

Created on 2021-12-20 by the reprex package (v2.0.1)

thanks Alexis, it's what I was looking for!

best,

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.