Sort Data Frame Column by Factor

Sort data frame column by factor

order takes multiple arguments, and it does just what you want:

with(score, score[order(sex, y, x),])
## x y sex
## 3 SUSAN 6.636370 F
## 5 EMMA 6.873445 F
## 9 VIOLET 8.539329 F
## 6 LEONARD 6.082038 M
## 2 TOM 7.812380 M
## 8 MATT 8.248374 M
## 4 LARRY 8.424665 M
## 7 TIM 8.754023 M
## 1 MARK 8.956372 M

How can I sort a dataframe by a predetermined order of factor levels in R?

We can specify the levels of the 'group' as category_order and that use that to `arrange

library(dplyr)
df1 <- df %>%
arrange(factor(group, levels = category_order))
df1
# group value
#1 tree 50
#2 house 2
#3 lake 1
#4 human 5

Or using fct_relevel

library(forcats)
df %>%
arrange(fct_relevel(group, category_order))

How can I arrange data.frame according to the factor levels?

Like this?

arrange(df, match(df$index, levels(df$index)))

index amount
1 a 76
2 b 10
3 e 60

Data

df<-data.frame(index=c("b","a","e"),amount=c(10,76,60))
df$index<-factor(df$index,levels=c("b","e","a"))

Sorting a factor column in R dataframe according to another factor column

Based on the comments and your expected output, try this. It is not entirely clear this is what you wanted.

library(dplyr)

df %>%
mutate(jk = sort(jk)) %>%
group_by(jk) %>%
mutate(jn = sort(jn))
# Groups:   jk [3]
jj jk jn
<fct> <fct> <fct>
1 an 0 0
2 al 0 1
3 ak 0 2
4 cj 1 0
5 bd 1 0
6 bi 1 1
7 bj 2 1
8 bn 2 1
9 bl 2 3

Sort a Dataframe using External Factor Ordering in R

We can convert the 'x_order' to integer and use as row index

df1 <- df[as.integer(x_order),]

In a general case, we can use match

df1 <- df[match(x_order, df$x),]

Or convert the 'x' column to factor with levels specified as 'x_orderand doorder`

df[order(factor(df$x, levels = x_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))) 

Order a data frame according to a given order

There are couple of options. One is to match the 'level' column with level.order' to get the numeric index and thenorder` based on it

df[order(match(df$level, level.order)),]

Or convert the 'level' to factor specifying the levels as 'level.order' and then order on it

df[order(factor(df$level, levels = level.order)),]

Order data.frame by factor based on integers

We can order the dataset rows after converting the 'b' column to numeric class, but the class of the 'b' still remains as factor.

sample1 <- sample[order(as.numeric(as.character(sample$b))),]
row.names(sample1) <- NULL
str(sample1)
#'data.frame': 5 obs. of 2 variables:
#$ a: num 1 3 4 2 2
#$ b: Factor w/ 5 levels "1","11","12",..: 1 4 5 2 3

sample1
# a b
#1 1 1
#2 3 2
#3 4 3
#4 2 11
#5 2 12

sorting dataframe by one factor column ascending another factor descending

You could also use the order function with method = "radix" which allows you to pass a vector for the argument decreasing:

## Generate sample data:
set.seed(123)
dat <- data.frame(a = letters[sample(1:5, 20, replace = TRUE)],
b = rep(c("orange", "apple", "pear", "banana"), 5))
## Sort by increasing a and decreasing b:
dat2 <- dat[order(dat$a, dat$b, decreasing = c(FALSE, TRUE), method = "radix"),]

head(dat2)
a b
15 a pear
6 a apple
18 a apple
19 b pear
1 b orange
17 b orange

Alternatively you could sort both columns in increasing order and just reverse column b using the function rev:

dat3 <- dat[order(dat$a, rev(dat$b)),]

head(dat3)
a b
15 a pear
6 a apple
18 a apple
19 b pear
1 b orange
17 b orange

ordering data frame based on factor levels indices in r

You could use dplyr as follows below. This is a variant of another answer, without using stringr.

library(dplyr)
df %>%
arrange(as.numeric(gsub("\\D+", "", string)))

## Name string value
## 1 BB a8 0.35120965
## 2 DD a8 0.54526648
## 3 BB a11 -0.90101120
## 4 AA a11 1.65637910
## 5 DD a45 0.42240082
## 6 CC a45 -0.30438594
## 7 AA a120 -0.05781699
## 8 AA a120 -1.83615123
## 9 DD a140 -1.82698618

You can also further sort by Name in addition to string.

so.df %>%
arrange(
as.numeric(gsub("\\D+", "", string)),
Name
)
## Name string value
## 1 BB a8 0.35120965
## 2 DD a8 0.54526648
## 3 AA a11 1.65637910
## 4 BB a11 -0.90101120
## 5 CC a45 -0.30438594
## 6 DD a45 0.42240082
## 7 AA a120 -0.05781699
## 8 AA a120 -1.83615123
## 9 DD a140 -1.82698618


Related Topics



Leave a reply



Submit