I want to convert a string column to factors, taking "NA" as an actual factor level (say "NA" means "North America"), and also taking "" as an actual factor level. However, both "" and <NA> are mapped to "NA" by parse_factor, while NC is mapped to <NA>.
library(tidyverse)
df<- tibble(
id=1:5,
group= c('NA', 'NB', 'NC', '', NA)
)
df %>%
mutate(f= parse_factor(group,
levels= c('', 'NA', 'NB'),
na=character()))
#> Warning: 1 parsing failure.#> row # A tibble: 1 x 4 col row col expected actual expected <int> <int> <chr> <chr> actual 1 3 NA value in level set NC#> # A tibble: 5 x 3#> id group f #> <int> <chr> <fct>#> 1 1 NA NA #> 2 2 NB NB #> 3 3 NC <NA> #> 4 4 "" NA #> 5 5 <NA> NA
But I expected
#> # A tibble: 5 x 3#> id group f #> <int> <chr> <fct>#> 1 1 NA NA #> 2 2 NB NB #> 3 3 NC <NA> #> 4 4 "" ""#> 5 5 <NA> <NA>
The text was updated successfully, but these errors were encountered:
djbirke
changed the title
parse_factor implictly relevels empty string "" and actual NA to factor "NA"
parse_factor implicitly relevels empty string "" and actual NA to factor "NA"
Jun 12, 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/
lockbot
locked and limited conversation to collaborators
May 13, 2019
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
readr_1.1.1
andtidyverse_1.2.1
I want to convert a string column to factors, taking
"NA"
as an actual factor level (say"NA"
means "North America"), and also taking""
as an actual factor level. However, both""
and<NA>
are mapped to"NA"
byparse_factor
, whileNC
is mapped to<NA>
.But I expected
The text was updated successfully, but these errors were encountered: