Gistlib Logo

gistlib

sort row order as the column order in a matrix in r

# Create a matrix
mat <- matrix(1:12, nrow = 3, byrow = TRUE)

# Sort row order as column order
sorted_mat <- mat[,order(col(mat))]

sorted_mat
144 chars
8 lines

The sorted_mat will be the matrix with row order as the column order.

similar r code snippets

find the kth least frequent element in an array in r
find the kth most frequent element in an array in r
sort a dataframe according to a specfic variable pattern in r
find the kth most common element in an array in r
find the kth largest element in an array in r
find the kth least common element in an array in r
load a spreadsheet into r. combined the first four rows and six columns into a matrix and if there are duplicate values in the matrix, randomise all rows in r
sort rows with missing values in r
find the kth smallest element in an array in r
sort an array in r

related categories

r
matrix
sorting

gistlibby LogSnag