How to reorder two columns while keeping another column unchanged

Hi @kartik.kaushal!

I think the way to think about this problem is that you want to arrange your data so that Day_number, Month_number, and Address are all in ascending order. In a tidy data frame, each row represents a single observation, while the columns are different pieces of data ("variables" or "features") about that observation. So if you tried to change the order of some columns but not others, you would be breaking the connection between the values that all describe the same observation, and therefore mixing up your data.

I don't quite know what "ascending order" means for your Address variable. If it's something other than lexical order, then you will need to separately define what the special order you want is. For now, I'll assume it's just lexical order.

Working from a variation on the example data in your other question:

library(tidyverse)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date

# I changed some values here to make the effect of re-arranging
# the data clearer
example_data <- "Date_Time,Address,Consumption
19/4/2016 13:00,41-2TV,40
20/4/2016 14:00,41-2TV,75
21/5/2016 15:00,41-2TV,35
19/4/2016 13:00,39-2TV,70
20/5/2016 14:00,39-2TV,80
21/4/2016 15:00,39-2TV,10
19/5/2016 13:00,43-2TV,10
20/4/2016 14:00,43-2TV,25
21/4/2016 15:00,43-2TV,40"

DHW_DayStatsHotwater <- read_csv(example_data) %>% 
  mutate(
    Date_Time = dmy_hm(Date_Time),
    Day_number = day(Date_Time),
    Month_number = month(Date_Time)
  ) %>% 
  # Mix up the order of rows for the sake of demonstration
  arrange(desc(Consumption))

DHW_DayStatsHotwater
#> # A tibble: 9 x 5
#>   Date_Time           Address Consumption Day_number Month_number
#>   <dttm>              <chr>         <dbl>      <int>        <dbl>
#> 1 2016-05-20 14:00:00 39-2TV           80         20            5
#> 2 2016-04-20 14:00:00 41-2TV           75         20            4
#> 3 2016-04-19 13:00:00 39-2TV           70         19            4
#> 4 2016-04-19 13:00:00 41-2TV           40         19            4
#> 5 2016-04-21 15:00:00 43-2TV           40         21            4
#> 6 2016-05-21 15:00:00 41-2TV           35         21            5
#> 7 2016-04-20 14:00:00 43-2TV           25         20            4
#> 8 2016-04-21 15:00:00 39-2TV           10         21            4
#> 9 2016-05-19 13:00:00 43-2TV           10         19            5

# Re-arrange rows
DHW_DayStatsHotwater %>% arrange(Address, Month_number, Day_number)
#> # A tibble: 9 x 5
#>   Date_Time           Address Consumption Day_number Month_number
#>   <dttm>              <chr>         <dbl>      <int>        <dbl>
#> 1 2016-04-19 13:00:00 39-2TV           70         19            4
#> 2 2016-04-21 15:00:00 39-2TV           10         21            4
#> 3 2016-05-20 14:00:00 39-2TV           80         20            5
#> 4 2016-04-19 13:00:00 41-2TV           40         19            4
#> 5 2016-04-20 14:00:00 41-2TV           75         20            4
#> 6 2016-05-21 15:00:00 41-2TV           35         21            5
#> 7 2016-04-20 14:00:00 43-2TV           25         20            4
#> 8 2016-04-21 15:00:00 43-2TV           40         21            4
#> 9 2016-05-19 13:00:00 43-2TV           10         19            5

Created on 2019-10-24 by the reprex package (v0.3.0)