SarinaAtkinson-NOAA / SEFSC-ODM-Management-History

A method for generating an analysis-ready version of the Management History Dataset
Other
0 stars 0 forks source link

Review adjusting end day #9

Closed SarinaAtkinson-NOAA closed 1 year ago

SarinaAtkinson-NOAA commented 1 year ago

Discussed in https://github.com/SarinaAtkinson-NOAA/SEFSC-ODM-Management-History/discussions/8

Originally posted by **SarinaAtkinson-NOAA** November 3, 2022 https://github.com/SarinaAtkinson-NOAA/SEFSC-ODM-Management-History/blob/3d7504dbf2e5901321ee3c1b9c33c0d690329ce5/code/MH01_new_variables.R#L151 Why are we adjusting the end day to the prior day for only STATUS_TYPE == "RECURRING"? How are cases when the day = 1 being adjusted to the prior day being the last day of the previous month?
SarinaAtkinson-NOAA commented 1 year ago

Reorder all of the mutates to create start date and end date to have smarter, more understandable code. Create a check to summarize the changes and overwriting of dates so we know what changes were made.

Reg 858 example we can test.

SarinaAtkinson-NOAA commented 1 year ago

@AdyanRios-NOAA will tackle the start and end day of the week add/subtract issue

AdyanRios-NOAA commented 1 year ago

I found a forcats function fct_shift() that I thought would do the trick, but it more related to reordering for visuals. I could not find how to shift a factor by one. So I wrote code to do it. It is not very slick, but it works. I am going to put it into a branch to see what we think.

AdyanRios-NOAA commented 1 year ago

Create an ordered factor x <- factor(c("Mon", "Tue", "Wed"), levels = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"), ordered = TRUE)

x [1] Mon Tue Wed Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat

Convert to numeric: y = as.numeric(x)

y [1] 2 3 4

Add one to everyone: z = y+1

z [1] 3 4 5

Redefine ordered factor (one level lower) i = z i[1] = i[1]-1

i [1] 2 4 5

Return to Factor j = factor(i,
levels = c("1", "2", "3", "4", "5", "6", "7"), ordered = TRUE)

j [1] 2 4 5 Levels: 1 < 2 < 3 < 4 < 5 < 6 < 7

Return to days of the week: k=j levels(k) = c("Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri")

k [1] Sun Tue Wed Levels: Sat < Sun < Mon < Tue < Wed < Thu < Fri

AdyanRios-NOAA commented 1 year ago

@SarinaAtkinson-NOAA

If you get a chance to review my latest commit and to practice branch merges ahead of Friday please give it a shot! The changes should make sense with the notes in the code.

recode_factor()ended up being a somewhat ok solution! A bit cleaner than what I had suggested in the comment above.

SarinaAtkinson-NOAA commented 1 year ago

@AdyanRios-NOAA looks good! I only made one small change (see commit above). The change was only to retain original fields rather than recoding to numeric.