I don’t know if there is a more idiomatic way, but it looks like what you want:
A <- matrix(c(3, NA, NA, 4, 2, NA, 1, 5, 3), nrow = 3)
A
[,1] [,2] [,3]
[1,] 3 4 1
[2,] NA 2 5
[3,] NA NA 3
t(apply(A, 1, function(x) x[order(is.na(x))]))
[,1] [,2] [,3]
[1,] 3 4 1
[2,] 2 5 NA
[3,] 3 NA NA
A <- matrix(c(3, NA, NA, NA, 4, 2, NA, NA, 1, 5, 3, NA), nrow=4)
A
[,1] [,2] [,3]
[1,] 3 4 1
[2,] NA 2 5
[3,] NA NA 3
[4,] NA NA NA
t(apply(A, 1, function(x) x[order(is.na(x))]))
[,1] [,2] [,3]
[1,] 3 4 1
[2,] 2 5 NA
[3,] 3 NA NA
[4,] NA NA NA
A <- matrix(c(3, NA, NA, 4, 2, NA, 1, 5, 3, 6, 7, 8), nrow=3)
A
[,1] [,2] [,3] [,4]
[1,] 3 4 1 6
[2,] NA 2 5 7
[3,] NA NA 3 8
t(apply(A, 1, function(x) x[order(is.na(x))]))
[,1] [,2] [,3] [,4]
[1,] 3 4 1 6
[2,] 2 5 7 NA
[3,] 3 8 NA NA