It is also possible to perform this action with the package data.table
.
# df exemplo
set.seed(1)
LL <- c(sample(1:10, size = 2, replace = T),
sample(1:10, size = 5, replace = T))
base <- data.frame(work_clss = c(LETTERS[1:10],
LETTERS[LL]),
id = 1:17,
n = sample(1:100, size = 17, replace = T))
library(data.table)
base <- data.table::data.table(base)
base[order(rank(work_clss))]
base[order(work_clss, decreasing = FALSE),]
base[order(-rank(work_clss))]
base[order(work_clss, decreasing = TRUE),]
Considering unique(base$workclass)
it is necessary to transform this information into class data frame and then turn it into data table.:
Ex.: Using the fictitious data table.
library(data.table)
library(dplyr)
set.seed(1)
LL <- c(sample(1:10, size = 2, replace = T),
sample(1:10, size = 5, replace = T))
base <- data.frame(workclass = c(LETTERS[1:10],
LETTERS[LL]),
id = 1:17,
n = sample(1:100, size = 17, replace = T))
table(base$workclass) # verificando as letras/palavras/fatores repetidos
> table(base$workclass)
A B C D E F G H I J
1 1 3 2 1 2 1 1 2 3
df <- as.data.frame(unique(base$workclass)) # transformando em data frame
colnames(df) <- "workclass" # ajustando o nome da coluna
df <- data.table::data.table(df) # transformando em data.table
df[order(rank(workclass))] # Ajustando em ordem crescente
> df[order(rank(workclass))]
workclass
1: A
2: B
3: C
4: D
5: E
6: F
7: G
8: H
9: I
10: J
table(df$workclass)
> table(df$workclass)
A B C D E F G H I J
1 1 1 1 1 1 1 1 1 1
yes, but how I integrate with Unique (base$workclass)*** shows me all the records . but I want to show them in order
– alexoab oab
I will edit the answer and in the final part I will put a didactic example.
– bbiasi
If you have any questions, check this link and provide data to the point where your problem is reproducible.
– bbiasi
thanks helped a lot!
– alexoab oab