Sort data by two conditions

Hello everybody,
I have a data set like that. I would like to sort by two conditions with two variables, namely "date" and "stock". ("stock" sorted from A to Z , "date" sorted from oldest to newest). I tried to sort but I could not combine the two conditions and got the wrong output. The data was sorted with only the day, not by month and year.
I would appreciate any helps.
Thank you in advance.

data<-data.frame(
    X = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L),
    closed_price = c(33100L, 33300L, 34100L, 34400L, 33600L, 33200L,
                     32200L, 33400L, 34100L, 34600L),
    opened_price = c(33600L, 33900L, 34400L, 33900L, 34000L, 32200L,
                     32800L, 34500L, 34600L, 35500L),
    adjust_closed_price = c(8591L, 8643L, 8850L, 8928L, 8721L, 8617L, 8357L,
                            8669L, 8850L, 8980L),
    date = as.factor(c("31-12-10", "30-12-10", "29-12-11",
                       "31-12-11", "29-12-10", "24-12-10",
                       "23-12-11", "22-12-11", "21-12-12", "20-12-12")),
    stock = as.factor(c("AAA", "AAA", "AAA", "AAA", "AAC", "AAC",
                        "AAC", "AAC", "AAC", "AAC"))
)

Hi, can you provide the code you tried already ? In you example, date column is a factor not a date object. It will be order as a factor and not as a date. You can convert easily with a :package: like lubridate or using R base function as.Date then order.

Examples:

date = as.factor(c("31-12-10", "30-12-10", "29-12-11",
                   "31-12-11", "29-12-10", "24-12-10",
                   "23-12-11", "22-12-11", "21-12-12", "20-12-12"))
sort(date)                 
#>  [1] 20-12-12 21-12-12 22-12-11 23-12-11 24-12-10 29-12-10 29-12-11
#>  [8] 30-12-10 31-12-10 31-12-11
#> 10 Levels: 20-12-12 21-12-12 22-12-11 23-12-11 24-12-10 ... 31-12-11

# converting to date
date2 = as.Date(as.character(date), format = "%d-%m-%y")
sort(date2)
#>  [1] "2010-12-24" "2010-12-29" "2010-12-30" "2010-12-31" "2011-12-22"
#>  [6] "2011-12-23" "2011-12-29" "2011-12-31" "2012-12-20" "2012-12-21"

date3 = lubridate::dmy(as.character(date))
sort(date3)
#>  [1] "2010-12-24" "2010-12-29" "2010-12-30" "2010-12-31" "2011-12-22"
#>  [6] "2011-12-23" "2011-12-29" "2011-12-31" "2012-12-20" "2012-12-21"

Created on 2019-11-18 by the reprex package (v0.3.0)

Also, for data wrangling, take a look a dplyr :package:. There is an arrange function that will help you.


1 Like

I used this code
data_sorted = data[order(data$date),]
or
data_sorted = data[order(data[,6], data[, 7]),]

Thanks. I think you should transform to Date object instead of using factor for date, especially if you want to order by date component. Look at the advice above.

Hope it helps.

1 Like

I have done it with the code you suggested. Thank you so much.

data$date2 = as.Date(as.character(data$date), format = "%d-%m-%y")
sort(data$date2)
data$date3 = lubridate::dmy(as.character(data$date))
sort(data$date3)
data_sorted<-data%>% arrange(stock, date3)

Glad it works !

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.