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

trim numerical vector? #2108

Closed
einarhjorleifsson opened this issue Sep 2, 2016 · 2 comments
Closed

trim numerical vector? #2108

einarhjorleifsson opened this issue Sep 2, 2016 · 2 comments

Comments

@einarhjorleifsson
Copy link

worth a thought?? in line with between. and str_trim. below also would return any NA's

trim <- function(x, left, right) {

  # test if value is a number
  # ...

  if(missing(left)) left = min(x, na.rm = TRUE)
  if(missing(right)) right = max(x, na.rm = TRUE)

  x <- ifelse(x < left, left, x)
  x <- ifelse(x > right, right, x)

  return(x)
}

# example
data_frame(x = c(1:10, NA)) %>% 
  mutate(x1 = trim(x, 4, 6),
         x2 = trim(x, 4),
         x3 = trim(x, right = 6))

einar

@krlmlr
Copy link
Member

krlmlr commented Nov 7, 2016

@hadley: pmin(pmax(x, min_val), max_val) -- perhaps in vctrs?

@hadley
Copy link
Member

hadley commented Nov 7, 2016

Yeah, vctrs is the right place. Can you please move there?

@krlmlr krlmlr closed this as completed Dec 15, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Jun 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants