英文:
Label missing values as any other strings
问题
我使用 haven
创建了一个带标签的列。
library(dplyr)
library(haven)
df <- tibble(x = labelled(c(1:3, NA), c('a' = 1, 'b' = 2, 'missing' = NA)))
# # A tibble: 4 × 1
# x
# <int+lbl>
# 1 1 [a]
# 2 2 [b]
# 3 3
# 4 NA
似乎缺失值未被标记。我期望看到类似于以下内容:
# # A tibble: 4 × 1
# x
# <int+lbl>
# 1 1 [a]
# 2 2 [b]
# 3 3
# 4 NA [missing]
此外,使用 as_factor
会得到以下结果:
as_factor(df$x)
# [1] a b 3 <NA>
# Levels: a b 3 missing
但我想要以下结果:
# [1] a b 3 missing
# Levels: a b 3 missing
英文:
I create a labelled column with haven
.
library(dplyr)
library(haven)
df <- tibble(x = labelled(c(1:3, NA), c('a' = 1, 'b' = 2, 'missing' = NA)))
# # A tibble: 4 × 1
# x
# <int+lbl>
# 1 1 [a]
# 2 2 [b]
# 3 3
# 4 NA
I seems that the missing value fails to be labelled. I expect to see somthing like
# # A tibble: 4 × 1
# x
# <int+lbl>
# 1 1 [a]
# 2 2 [b]
# 3 3
# 4 NA [missing]
In addition, as_factor
gives
as_factor(df$x)
# [1] a b 3 <NA>
# Levels: a b 3 missing
but I want
# [1] a b 3 missing
# Levels: a b 3 missing
答案1
得分: 3
With haven::tagged_na
:
x <- labelled(c(1:3, tagged_na("x")), c('a' = 1, 'b' = 2, "missing" = tagged_na("x")))
<labelled<double>[4]>
[1] 1 2 3 NA(x)
Labels:
value label
1 a
2 b
NA(x) missing
Works as intended with as_factor
:
as_factor(x)
#[1] a b 3 missing
#Levels: a b 3 missing
英文:
With haven::tagged_na
:
x <- labelled(c(1:3, tagged_na("x")), c('a' = 1, 'b' = 2, "missing" = tagged_na("x")))
<labelled<double>[4]>
[1] 1 2 3 NA(x)
Labels:
value label
1 a
2 b
NA(x) missing
Works as intended with as_factor
:
as_factor(x)
#[1] a b 3 missing
#Levels: a b 3 missing
答案2
得分: 1
我们可以使用forcats
包中的fct_explicit_na()
函数:
library(dplyr)
library(haven)
library(forcats)
df1 <- df %>%
mutate(x = fct_explicit_na(as_factor(x), na_level = "missing"))
levels(df1$x)
[1] "a" "b" "3" "missing"
英文:
We could use fct_explicit_na()
from forcats
package:
library(dplyr)
library(haven)
library(forcasts)
df1 <- df %>%
mutate(x = fct_explicit_na(as_factor(x), na_level = "missing"))
levels(df1$x)
[1] "a" "b" "3" "missing"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论