无法解析 "Warning: Factor `officialTitle` contains implicit NA, consider using `forcats::fct_explicit_na`"
Cannot resolve "Warning: Factor `officialTitle` contains implicit NA, consider using `forcats::fct_explicit_na`"
我尝试了以下方法。为什么我仍然收到 forcats
警告?
#1 我执行
plotdata <- dplyr::filter(Marriage, FALSE) %>% count(officialTitle)
if (nrow(plotdata) > 0){
str <- paste("nrows > 0. Number of rows is ", nrow(plotdata))
print(str)
print(plotdata)
但我得到以下信息:
[1] "nrows > 0. Number of rows is 1"
# A tibble: 1 x 2
officialTitle n
<fct> <int>
1 NA 0
Warning message:
Factor `officialTitle` contains implicit NA, consider using `forcats::fct_explicit_na`
#2 我注意到警告并执行以下
plotdata <- dplyr::filter(Marriage, FALSE) %>% count(officialTitle)
plotdata$officialTitle <- fct_explicit_na(plotdata$officialTitle)
if (nrow(plotdata) > 0){
str <- paste("nrows > 0. Number of rows is ", nrow(plotdata))
print(str)
print(plotdata)
这是结果:
[1] "nrows > 0. Number of rows is 1"
# A tibble: 1 x 2
officialTitle n
<fct> <int>
1 (Missing) 0
Warning message:
Factor `officialTitle` contains implicit NA, consider using `forcats::fct_explicit_na`
#3 那我想这可能跟forcats
没关系吧。我删除了 forcats
语句并从 plotdata 中删除了 na,即
plotdata <- dplyr::filter(Marriage, FALSE) %>% count(officialTitle)
plotdata <- na.omit(plotdata)
if (nrow(plotdata) > 0){
str <- paste("nrows > 0. Number of rows is ", nrow(plotdata))
print(str)
print(plotdata)
输出如下:
[1] "nrows < 0. Number of rows is 0"
# A tibble: 0 x 2
# ... with 2 variables: officialTitle <fct>, n <int>
Warning message:
Factor `officialTitle` contains implicit NA, consider using `forcats::fct_explicit_na`
完整代码:
# Load packages ----
library(shiny)
library(ggplot2)
library(dplyr)
library(scales)
library(treemapify)
library(forcats)
# Load data ----
data(Marriage, package="mosaicData")
plotdata <- dplyr::filter(Marriage, FALSE) %>% count(officialTitle)
#plotdata$officialTitle <- fct_explicit_na(plotdata$officialTitle)
plotdata <- na.omit(plotdata)
if (nrow(plotdata) > 0){
str <- paste("nrows > 0. Number of rows is ", nrow(plotdata))
print(str)
print(plotdata)
ggplot(plotdata,
aes(fill = officialTitle,
area = n,
label = officialTitle)) +
geom_treemap() +
geom_treemap_text(colour = "white",
place = "centre") +
labs(title = "Marriages by officiate") +
theme(legend.position = "none")
} else {
str <- paste("nrows < 0. Number of rows is ", nrow(plotdata))
print(str)
print(plotdata)
}
错误消息不是由 forcats
引发的。在您的情况下,它是 count
ing 空 df 的结果。试试这个:
# Empty dataframe
df <- data.frame(x = factor(integer(0)))
# Count empty column
dplyr::count(df, x)
#> Warning: Factor `x` contains implicit NA, consider using
#> `forcats::fct_explicit_na`
#> # A tibble: 1 x 2
#> x n
#> <fct> <int>
#> 1 <NA> 0
# To avoid the warning use .drop = FALSE which also results in an empty df
dplyr::count(df, x, .drop = FALSE)
#> # A tibble: 0 x 2
#> # ... with 2 variables: x <fct>, n <int>
由 reprex package (v0.3.0)
于 2020-04-14 创建
使用 Marriage
数据集:
# Load data ----
library(dplyr)
data(Marriage, package="mosaicData")
dplyr::filter(Marriage, FALSE) %>% count(officialTitle)
#> Warning: Factor `officialTitle` contains implicit NA, consider using
#> `forcats::fct_explicit_na`
#> # A tibble: 1 x 2
#> officialTitle n
#> <fct> <int>
#> 1 <NA> 0
dplyr::filter(Marriage, FALSE) %>% count(officialTitle, .drop = FALSE)
#> # A tibble: 9 x 2
#> officialTitle n
#> <fct> <int>
#> 1 "BISHOP" 0
#> 2 "CATHOLIC PRIEST" 0
#> 3 "CHIEF CLERK" 0
#> 4 "CIRCUIT JUDGE " 0
#> 5 "ELDER" 0
#> 6 "MARRIAGE OFFICIAL" 0
#> 7 "MINISTER" 0
#> 8 "PASTOR" 0
#> 9 "REVEREND" 0
由 reprex package (v0.3.0)
于 2020-04-14 创建
我尝试了以下方法。为什么我仍然收到 forcats
警告?
#1 我执行
plotdata <- dplyr::filter(Marriage, FALSE) %>% count(officialTitle)
if (nrow(plotdata) > 0){
str <- paste("nrows > 0. Number of rows is ", nrow(plotdata))
print(str)
print(plotdata)
但我得到以下信息:
[1] "nrows > 0. Number of rows is 1"
# A tibble: 1 x 2
officialTitle n
<fct> <int>
1 NA 0
Warning message:
Factor `officialTitle` contains implicit NA, consider using `forcats::fct_explicit_na`
#2 我注意到警告并执行以下
plotdata <- dplyr::filter(Marriage, FALSE) %>% count(officialTitle)
plotdata$officialTitle <- fct_explicit_na(plotdata$officialTitle)
if (nrow(plotdata) > 0){
str <- paste("nrows > 0. Number of rows is ", nrow(plotdata))
print(str)
print(plotdata)
这是结果:
[1] "nrows > 0. Number of rows is 1"
# A tibble: 1 x 2
officialTitle n
<fct> <int>
1 (Missing) 0
Warning message:
Factor `officialTitle` contains implicit NA, consider using `forcats::fct_explicit_na`
#3 那我想这可能跟forcats
没关系吧。我删除了 forcats
语句并从 plotdata 中删除了 na,即
plotdata <- dplyr::filter(Marriage, FALSE) %>% count(officialTitle)
plotdata <- na.omit(plotdata)
if (nrow(plotdata) > 0){
str <- paste("nrows > 0. Number of rows is ", nrow(plotdata))
print(str)
print(plotdata)
输出如下:
[1] "nrows < 0. Number of rows is 0"
# A tibble: 0 x 2
# ... with 2 variables: officialTitle <fct>, n <int>
Warning message:
Factor `officialTitle` contains implicit NA, consider using `forcats::fct_explicit_na`
完整代码:
# Load packages ----
library(shiny)
library(ggplot2)
library(dplyr)
library(scales)
library(treemapify)
library(forcats)
# Load data ----
data(Marriage, package="mosaicData")
plotdata <- dplyr::filter(Marriage, FALSE) %>% count(officialTitle)
#plotdata$officialTitle <- fct_explicit_na(plotdata$officialTitle)
plotdata <- na.omit(plotdata)
if (nrow(plotdata) > 0){
str <- paste("nrows > 0. Number of rows is ", nrow(plotdata))
print(str)
print(plotdata)
ggplot(plotdata,
aes(fill = officialTitle,
area = n,
label = officialTitle)) +
geom_treemap() +
geom_treemap_text(colour = "white",
place = "centre") +
labs(title = "Marriages by officiate") +
theme(legend.position = "none")
} else {
str <- paste("nrows < 0. Number of rows is ", nrow(plotdata))
print(str)
print(plotdata)
}
错误消息不是由 forcats
引发的。在您的情况下,它是 count
ing 空 df 的结果。试试这个:
# Empty dataframe
df <- data.frame(x = factor(integer(0)))
# Count empty column
dplyr::count(df, x)
#> Warning: Factor `x` contains implicit NA, consider using
#> `forcats::fct_explicit_na`
#> # A tibble: 1 x 2
#> x n
#> <fct> <int>
#> 1 <NA> 0
# To avoid the warning use .drop = FALSE which also results in an empty df
dplyr::count(df, x, .drop = FALSE)
#> # A tibble: 0 x 2
#> # ... with 2 variables: x <fct>, n <int>
由 reprex package (v0.3.0)
于 2020-04-14 创建使用 Marriage
数据集:
# Load data ----
library(dplyr)
data(Marriage, package="mosaicData")
dplyr::filter(Marriage, FALSE) %>% count(officialTitle)
#> Warning: Factor `officialTitle` contains implicit NA, consider using
#> `forcats::fct_explicit_na`
#> # A tibble: 1 x 2
#> officialTitle n
#> <fct> <int>
#> 1 <NA> 0
dplyr::filter(Marriage, FALSE) %>% count(officialTitle, .drop = FALSE)
#> # A tibble: 9 x 2
#> officialTitle n
#> <fct> <int>
#> 1 "BISHOP" 0
#> 2 "CATHOLIC PRIEST" 0
#> 3 "CHIEF CLERK" 0
#> 4 "CIRCUIT JUDGE " 0
#> 5 "ELDER" 0
#> 6 "MARRIAGE OFFICIAL" 0
#> 7 "MINISTER" 0
#> 8 "PASTOR" 0
#> 9 "REVEREND" 0
由 reprex package (v0.3.0)
于 2020-04-14 创建