I am trying to reorder a data frame in R. The data frame is a 264x264 correlation matrix. I have a numeric vector (1:264) which contains the order that I want the rows and columns of my data frame to match.
I've tried using df[order(list),]
and df[,order(list)]
to sort the rows and columns. While the rows and columns reorder, it is not to the expected order. For instance, if the first three columns were supposed to be 73, 85, and 66, I instead get 224, 95, and 135.
names(df) <- c(1:ncol(df))
row.names(df) <- c(1:nrow(df))
df2 <- df[order(order.vector),]
df2 <- df2[,order(order.vector)]
The rows and columns of the data frame should be in the same order as the numbers in my list (order.vector
).