Order Data Frame Rows According to Vector With Specific Order

Order data frame rows according to vector with specific order

Try match:

df <- data.frame(name=letters[1:4], value=c(rep(TRUE, 2), rep(FALSE, 2)))
target <- c("b", "c", "a", "d")
df[match(target, df$name),]

name value
2 b TRUE
3 c FALSE
1 a TRUE
4 d FALSE

It will work as long as your target contains exactly the same elements as df$name, and neither contain duplicate values.

From ?match:

match returns a vector of the positions of (first) matches of its first argument 
in its second.

Therefore match finds the row numbers that matches target's elements, and then we return df in that order.

How to arrange data rows in by specific order of a particular grouping variable level in r

We could convert to factor with levels specified as the vector objects created in arrange

library(dplyr)
mtcars1 <- mtcars %>%
arrange(factor(gear, levels = gear_order),
factor(carb, levels = carb_order))

Sorting the values inside row in a data frame, by the order of its factor levels?

In your code, the issue is happening at this line.

arcana_table <- as.data.frame(matrix(shuffled_arcana, nrow = 5, ncol = 5))

shuffled_arcana is a factored vector but you cannot have a factor-matrix so it changes the vector from factor to character and hence, sorting does not happen as desired.

Here's a way -

set.seed(2022)

arcanaVector <- c(rep("Supreme", 3),
rep(c("Good", "Moderate", "Poor", "Awful"), each = 5),
rep("Worst", 2))
arcanaLevels <- c("Supreme", "Good", "Moderate", "Poor", "Awful", "Worst")
shuffled_arcana <- sample(arcanaVector)
arcana_table <- matrix(shuffled_arcana,nrow = 5, ncol = 5)
row.names(arcana_table) <- c("Presence", "Manner", "Expression", "Complexity", "Tradition")

arcana_table <- apply(arcana_table, 1, function(x) sort(factor(x, arcanaLevels))) |>
t() |>
as.data.frame()

arcana_table

# V1 V2 V3 V4 V5
#Presence Good Good Good Good Awful
#Manner Supreme Moderate Poor Awful Awful
#Expression Supreme Supreme Moderate Moderate Poor
#Complexity Moderate Moderate Poor Poor Worst
#Tradition Good Poor Awful Awful Worst

If you want to change a specific row you may use -

arcana_table[1, ] <- as.character(sort(factor(arcana_table[1, ], arcanaLevels))) 

how to reorder a dataframe based on another vector in R

You can order your df using dplyr

library(dplyr)
df %>%
arrange(factor(BRANCH, levels = Branches))

R: Sort a data frame based on the order of a vector?

We can convert the 'id' column to factor with levels specified as the unique elements in the vector and then do the order

df[order(factor(df$id, levels=unique(desiredOrder))),]
# id col1 col2
#6 456 9 8
#7 456 2 7
#8 456 1 5
#9 456 3 8
#10 456 4 3
#1 123 3 7
#2 123 6 8
#3 123 4 5
#4 123 3 4
#5 123 8 6

How can i reorder data frame with many columns and rows according to vector with specific order?

Since the order is what matters, and you have duplicate combinations, I think it's easier to order both data frames, bind them, and then return to the original order:

df <- data.frame(Temperatura,
Concentracao,
Velocidade,
remov_SMO, remov_CO, remov_BE)


reorder_ids <- do.call(order, as.list(plan.person))
plan_ordered <- plan.person[reorder_ids,]
df_ordered <- df[do.call(order, as.list(df[, 1:3])),]

new_plan <- data.frame(plan_ordered, df_ordered[, -(1:3)])
new_plan <- new_plan[reorder_ids,] # restore original order
attributes(new_plan) <- attributes(plan.person)

I think new_plan should have what you want, but you might have to adjust column names.

EDIT: I would be particularly careful with the output attributes,
it seems to me that plan.person has quite a few extras added by FrF2,
and other functions might depend on them.

How to sort the row order according to number not character?

rownames are always stored as characters, if you want to sort them according to their numeric value you can change it to numeric and order.

df <- df[order(as.numeric(rownames(df))), , drop = FALSE]
df

# colSums(fake_with_noise_boundary)
#1 -3405
#2 -2089
#3 1605
#10 2304
#11 -4096
#12 474
#20 -3921
#21 -2590
#30 1317
#31 2804
#40 2934


Related Topics



Leave a reply



Submit