Skip to content

parse_factor trims x automatically but not levels #735

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

Closed
adrtod opened this issue Nov 3, 2017 · 3 comments
Closed

parse_factor trims x automatically but not levels #735

adrtod opened this issue Nov 3, 2017 · 3 comments
Labels
bug an unexpected problem or unintended behavior feature a feature request or enhancement

Comments

@adrtod
Copy link

adrtod commented Nov 3, 2017

parse_factor(x, levels) trims x automatically but not levels.
It is inconsistent with the factor function.
I don't know if this behavior should be fixed or documented but it can be confusing.

library(readr)

factor(c("a", "a "), levels = c("a")) %>% as.integer() # 1 NA
parse_factor(c("a", "a "), levels = c("a")) %>% as.integer() # 1 1

factor(c("a", "a "), levels = c("a ")) %>% as.integer() # NA 1
parse_factor(c("a", "a "), levels = c("a ")) %>% as.integer() # NA NA

factor(c("a", "a "), levels = c("a", "a ")) %>% as.integer() # 1 2
parse_factor(c("a", "a "), levels = c("a", "a ")) %>% as.integer() # 1 1

factor(c("a", "a "), levels = c("a ", "a")) %>% as.integer() # 2 1
parse_factor(c("a", "a "), levels = c("a ", "a")) %>% as.integer() # 2 2
@jimhester jimhester added the feature a feature request or enhancement label Dec 7, 2017
@jimhester
Copy link
Collaborator

I guess this would mean parse_factor should have a trim argument to control trimming.

@jimhester jimhester added the bug an unexpected problem or unintended behavior label Dec 11, 2017
@jimhester
Copy link
Collaborator

So the real issue here was the parse_() functions always trimmed all of their values before parsing. I added a trim_ws parameter to control this, so you can now get identical behavior to factor in these examples.

library(readr)
library(magrittr)

factor(c("a", "a "), levels = c("a")) %>% as.integer()
#> [1]  1 NA
parse_factor(c("a", "a "), levels = c("a"), trim_ws = FALSE) %>% as.integer()
#> Warning: 1 parsing failure.
#> row col           expected actual
#>   2  -- value in level set     a
#> [1]  1 NA

factor(c("a", "a "), levels = c("a ")) %>% as.integer()
#> [1] NA  1
parse_factor(c("a", "a "), levels = c("a "), trim_ws = FALSE) %>% as.integer()
#> Warning: 1 parsing failure.
#> row col           expected actual
#>   1  -- value in level set      a
#> [1] NA  1

factor(c("a", "a "), levels = c("a", "a ")) %>% as.integer()
#> [1] 1 2
parse_factor(c("a", "a "), levels = c("a", "a "), trim_ws = FALSE) %>% as.integer()
#> [1] 1 2

factor(c("a", "a "), levels = c("a ", "a")) %>% as.integer()
#> [1] 2 1
parse_factor(c("a", "a "), levels = c("a ", "a"), trim_ws = FALSE) %>% as.integer()
#> [1] 2 1

Created on 2017-12-11 by the reprex package (v0.1.1.9000).

@lock
Copy link

lock bot commented Sep 25, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Sep 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants