I want to sort two variables across an ID variable.
Here is the data I have,
id <- c(11, 11, 11, 11, 12, 12, 12, 12)
m <- c(12, 1, 3, 2, 1, 3, 2, 1)
y <- c(2019, 2020, 2020, 2020, 2020, 2020, 2020, 2021)
value <- c(1, 2, 3, 4, 5, 6, 7, 8)
data <- data.frame(id, m, y, value)
id m y value
1 11 12 2019 1
2 11 1 2020 2
3 11 3 2020 3
4 11 2 2020 4
5 12 1 2020 5
6 12 3 2020 6
7 12 2 2020 7
8 12 1 2021 8
I want to sort the month (m) and year(y) by the id
variable. The expected output should be,
id m y value
1 11 12 2019 1
2 11 1 2020 2
3 11 2 2020 4
4 11 3 2020 3
5 12 1 2020 5
6 12 2 2020 7
7 12 3 2020 6
8 12 1 2021 8
I am trying this line to sort the dataframe.
> data[order(ordered(id, unique(id)), m, y), ]
id m y value
2 11 1 2020 2
4 11 2 2020 4
3 11 3 2020 3
1 11 12 2019 1
5 12 1 2020 5
8 12 1 2021 8
7 12 2 2020 7
6 12 3 2020 6
This is not what I am expecting. Anyone here to correct my mistakes?