How to reorder two columns while keeping another column unchanged

Split from: How to calculate daily average of hourly data and for different variables at the same time


I have another problem here. Reprex example can be found below.

library(data.table)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:data.table':
#> 
#>     between, first, last
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(readr)
library(ggplot2)
library(tictoc)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:data.table':
#> 
#>     hour, isoweek, mday, minute, month, quarter, second, wday,
#>     week, yday, year
#> The following object is masked from 'package:base':
#> 
#>     date
library(gtools)
library(stringi)
library(tidyverse)

DHW_DayStatsHotwater %>% select(Address,Month_number,Day_number,DayAvg)
#> Error in eval(lhs, parent, parent): object 'DHW_DayStatsHotwater' not found

I want to arrange my Day_number column and Month_number column in ascending order. However, my address column should remain as it is, as you can see the Address column is already in ascending order. However the DayAvg would ofcourse change its order.
I have used the following but it didn't work out in my case.

DHW_DayStatsHotwater%>%arrange(Day_number,Month_number)

Please note that all my columns are in numeric form except the Address column which is character.

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

Sorry but we cannot see anything in you reprex... I think you failed to paste the correct one. Here the dataset is missing from your global environment...

1 Like

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)

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