readr_1.1.1 and tidyverse_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" 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>
readr_1.1.1andtidyverse_1.2.1I 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, whileNCis mapped to<NA>.But I expected