You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 1factor(c("a", "a "), levels= c("a ")) %>% as.integer() # NA 1
parse_factor(c("a", "a "), levels= c("a ")) %>% as.integer() # NA NAfactor(c("a", "a "), levels= c("a", "a ")) %>% as.integer() # 1 2
parse_factor(c("a", "a "), levels= c("a", "a ")) %>% as.integer() # 1 1factor(c("a", "a "), levels= c("a ", "a")) %>% as.integer() # 2 1
parse_factor(c("a", "a "), levels= c("a ", "a")) %>% as.integer() # 2 2
The text was updated successfully, but these errors were encountered:
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 NAfactor(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 1factor(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 2factor(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).
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/
lockbot
locked and limited conversation to collaborators
Sep 25, 2018
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
bugan unexpected problem or unintended behaviorfeaturea feature request or enhancement
parse_factor(x, levels)
trimsx
automatically but notlevels
.It is inconsistent with the
factor
function.I don't know if this behavior should be fixed or documented but it can be confusing.
The text was updated successfully, but these errors were encountered: