Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rescale() can expand ranges #467

Merged
merged 7 commits into from
Nov 14, 2023
Merged

rescale() can expand ranges #467

merged 7 commits into from
Nov 14, 2023

Conversation

strengejacke
Copy link
Member

@strengejacke strengejacke commented Nov 14, 2023

Similar to scales::expand_range(), rescale() can now also expand ranges:

library(datawizard)
x <- seq(5, 15, 2)
x
#> [1]  5  7  9 11 13 15
rescale(x, multiply = 1.1)
#> [1]  4.5  6.7  8.9 11.1 13.3 15.5
#> (original range = 5 to 15)

rescale(x, add = 1)
#> [1]  4.0  6.4  8.8 11.2 13.6 16.0
#> (original range = 5 to 15)

rescale(x, add = -2)
#> [1]  7.0  8.2  9.4 10.6 11.8 13.0
#> (original range = 5 to 15)

rescale(x, add = c(3, 10))
#> [1]  2.0  6.6 11.2 15.8 20.4 25.0
#> (original range = 5 to 15)

rescale(x, add = c(-3, 5))
#> [1]  8.0 10.4 12.8 15.2 17.6 20.0
#> (original range = 5 to 15)


d <- data.frame(x = seq(5, 15, 2), y = seq(5, 15, 2))

# multiply range of each column, expanding by 10%
rescale(d, multiply = 1.1)
#>      x    y
#> 1  4.5  4.5
#> 2  6.7  6.7
#> 3  8.9  8.9
#> 4 11.1 11.1
#> 5 13.3 13.3
#> 6 15.5 15.5

# add 0.5 to each end of range (same as 10% expanding)
rescale(d, add = 0.5)
#>      x    y
#> 1  4.5  4.5
#> 2  6.7  6.7
#> 3  8.9  8.9
#> 4 11.1 11.1
#> 5 13.3 13.3
#> 6 15.5 15.5

# multiply range of x by 1.1 and range of y by 0.5
rescale(d, multiply = list(x = 1.1, y = 0.5))
#>      x    y
#> 1  4.5  7.5
#> 2  6.7  8.5
#> 3  8.9  9.5
#> 4 11.1 10.5
#> 5 13.3 11.5
#> 6 15.5 12.5

# add 0.5 to each end of range of x and subtract 2.5 from each end of range of y
rescale(d, add = list(x = 0.5, y = -2.5))
#>      x    y
#> 1  4.5  7.5
#> 2  6.7  8.5
#> 3  8.9  9.5
#> 4 11.1 10.5
#> 5 13.3 11.5
#> 6 15.5 12.5

# add 1 to lower end of range of x and 3 to upper end of range of x
# add 2 to lower end of range of y and 4 to upper end of range of y
rescale(d, add = list(x = c(1, 3), y = c(2, 4)))
#>      x    y
#> 1  4.0  3.0
#> 2  6.8  6.2
#> 3  9.6  9.4
#> 4 12.4 12.6
#> 5 15.2 15.8
#> 6 18.0 19.0

Not sure, though, if add should add (subtract) the value to each end of the range? I.e. new range = c(min - add, max + add)? Then the range would expand by 2*add.

Copy link

codecov bot commented Nov 14, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (b2d3b1b) 89.03% compared to head (a81ae2d) 89.09%.

❗ Current head a81ae2d differs from pull request most recent head a6e73b8. Consider uploading reports for the commit a6e73b8 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #467      +/-   ##
==========================================
+ Coverage   89.03%   89.09%   +0.05%     
==========================================
  Files          72       72              
  Lines        5383     5409      +26     
==========================================
+ Hits         4793     4819      +26     
  Misses        590      590              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@strengejacke
Copy link
Member Author

@bwiernik @etiennebacher we could add an alias expand_range(), which only has arguments multiply and add (and not to, like rescale()), which then calls rescale().

Copy link
Member

@etiennebacher etiennebacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, minor things

R/data_rescale.R Outdated Show resolved Hide resolved
R/data_rescale.R Outdated Show resolved Hide resolved
@etiennebacher
Copy link
Member

etiennebacher commented Nov 14, 2023

Not sure, though, if add should add (subtract) the value to each end of the range?

Hard to say, I don't have a use case for this function in mind, I guess you have one?

we could add an alias expand_range()

wouldn't this create a conflict with scales::expand_range() (which is called a lot by ggplot2 IIRC)?

@strengejacke
Copy link
Member Author

strengejacke commented Nov 14, 2023

Not sure, though, if add should add (subtract) the value to each end of the range?

Hard to say, I don't have a use case for this function in mind, I guess you have one?

It seems that expected behaviour in this case would be to add add to min/max (I have changed this here already), see https://bsky.app/profile/tjmahr.com/post/3ke3nc3hix32u

we could add an alias expand_range()

wouldn't this create a conflict with scales::expand_range() (which is called a lot by ggplot2 IIRC)?

True. So no alias.

@etiennebacher
Copy link
Member

I'm not on bluesky, can you share a screenshot? (sorry I edited your post instead of writing a comment)

@strengejacke
Copy link
Member Author

Oh, I thought it would be visible for non-registered, too:
image

If you like, I can send you an invitation code.

@etiennebacher
Copy link
Member

If you like, I can send you an invitation code.

I'm fine on Mastodon for now, thanks ;)

@strengejacke
Copy link
Member Author

Social networks have become less important anyway, or have pretty much fallen asleep since Musk took over Twitter.

@strengejacke strengejacke merged commit b65df4d into main Nov 14, 2023
25 of 26 checks passed
@strengejacke strengejacke deleted the rescale_can_expand branch November 14, 2023 12:13
@etiennebacher
Copy link
Member

etiennebacher commented Nov 14, 2023

Social networks have become less important anyway, or have pretty much fallen asleep since Musk took over Twitter.

I don't know, I just can't understand the hype for bluesky, I don't see any reason why it will end up differently than twitter, but anyway that's not the topic 😄

@bwiernik
Copy link
Contributor

Personally I like Threads a lot among the various options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants