0

I have the dataframe below:

    product<-c("asd","saf","asd","bc","asd","saf","bc","saf")
date<-as.Date(c("2018-07-29","2018-07-25","2018-07-28","2018-06-29","2018-07-21","2018-07-12","2018-02-28","2018-07-10"))
p<-data.frame(product,date)

What I want to achieve is sort this dataframe by product name firstly and then by date without ruining the initial sorting.

I use:

p <- p %>% arrange(product)

to sort by name but when I use:

p<-p[order(as.Date(p$date, format="%Y/%m/%d")),]

the name sorting is lost.

An acceptable solution would be:

product       date
1     asd 2018-07-25
2     asd 2018-07-28
3     asd 2018-07-29
4     saf 2018-02-28
5     saf 2018-06-29
6     saf 2018-07-10
7      bc 2018-02-28
8      bc 2018-07-12

as I do not care about alphabetical sorting between names.

firmo23
  • 7,490
  • 2
  • 38
  • 114

2 Answers2

1

Using sqldf:

library(sqldf)
sqldf("select * from p order by product,date")

Output:

  product       date
1     asd 2018-07-21
2     asd 2018-07-28
3     asd 2018-07-29
4      bc 2018-02-28
5      bc 2018-06-29
6     saf 2018-07-10
7     saf 2018-07-12
8     saf 2018-07-25
Saurabh Chauhan
  • 3,161
  • 2
  • 19
  • 46
1

Basically ordering taking into account both columns:

p[order(p$product,p$date),]

Output:

  product       date
5     asd 2018-07-21
3     asd 2018-07-28
1     asd 2018-07-29
7      bc 2018-02-28
4      bc 2018-06-29
8     saf 2018-07-10
6     saf 2018-07-12
2     saf 2018-07-25

I believe this question is already repeated. Indeed, just in case I put it here, it has much more information: How to sort a dataframe by multiple column(s)?

Maik
  • 170
  • 7