I have a dataframe, and I would like to sort the rows into a custom order using a vector of row numbers. However, when I try to do this, my dataframe gets converted into a vector. How can I keep it as a dataframe? Here's a simplified example, but my real data has multiple columns, each with a different variable.
library(tibble)
# Simplified dataframe with a single numerical variable (in reality there are more variables, each in a separate column)
my_df <- data.frame(name = c(paste("sample", seq(1:12), sep = "_")),
treatment = c(rep(1, 4),
rep(2, 5),
rep(1, 3))) %>%
tibble::column_to_rownames("name")
# The order I want the rows to be in
sample_order <- c(1, 2, 3, 5, 4, 6, 7, 8, 9, 10, 11, 12)
# Attempt at changing the row order converts the dataframe into a vector
sorted_df <- my_df[sample_order,]