How to order Ascending and descending in R?

Asked

Viewed 5,241 times

2

Let’s assume

unique(base$workclass)#Checando os valores dos atributos das colunas, instâcias,linhas ou registros  
unique(base$workclass)# o resultado de apenas unico valor como seria a  ordem decrescente ou crescente :(
[1]  State-gov         Self-emp-not-inc  Private         
[4]  Federal-gov       Local-gov         Self-emp-inc    
[7]  Without-pay     
7 Levels:  Federal-gov  Local-gov  Private ...  Without-pay

2 answers

3

You can use the function sort of R base:

sort(base$workclass)

To sort decreasingly, you need to insert the argument decreasing = TRUE:

sort(base$workclass, decreasing = TRUE)

You can also use the function arrange of dplyr. However, it can only be used in data.frames or tibbles. Consider the data set below:

data <- data.frame(
  a = c('d', 'j', 'a', 'k'), 
  b = 1:4
)

Sort (by the variable a) increasingly:

library(dplyr)

data %>% 
  arrange(a)

#  a b
#1 a 3
#2 d 1
#3 j 2
#4 k 4

Decreasingly:

data %>% 
  arrange(desc(a))

#  a b
#1 k 4
#2 j 2
#3 d 1
#4 a 3

2

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)
  • work_class crescent
base[order(rank(work_clss))]

base[order(work_clss, decreasing = FALSE),]
  • work_class decreasing
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

  • I will edit the answer and in the final part I will put a didactic example.

  • If you have any questions, check this link and provide data to the point where your problem is reproducible.

  • 1

    thanks helped a lot!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.