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

parse_factor trims x automatically but not levels #735

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

Comments

@adrtod
Copy link

@adrtod 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
Copy link
Member

@jimhester jimhester commented Dec 7, 2017

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

Loading

@jimhester jimhester added the bug label Dec 11, 2017
@jimhester
Copy link
Member

@jimhester jimhester commented Dec 11, 2017

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).

Loading

@jimhester jimhester closed this in 27673d0 Dec 11, 2017
@lock
Copy link

@lock 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/

Loading

@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.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants