将缺失的数值标记为任何其他字符串。

huangapple go评论111阅读模式
英文:

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 &lt;- tibble(x = labelled(c(1:3, NA), c(&#39;a&#39; = 1, &#39;b&#39; = 2, &#39;missing&#39; = NA)))

# # A tibble: 4 &#215; 1
#           x
#   &lt;int+lbl&gt;
# 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 &#215; 1
#           x
#   &lt;int+lbl&gt;
# 1     1 [a]
# 2     2 [b]
# 3     3
# 4    NA [missing]

In addition, as_factor gives

as_factor(df$x)

# [1] a    b    3    &lt;NA&gt;
# 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 &lt;- labelled(c(1:3, tagged_na(&quot;x&quot;)), c(&#39;a&#39; = 1, &#39;b&#39; = 2, &quot;missing&quot; = tagged_na(&quot;x&quot;)))

&lt;labelled&lt;double&gt;[4]&gt;
[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 &lt;- labelled(c(1:3, tagged_na(&quot;x&quot;)), c(&#39;a&#39; = 1, &#39;b&#39; = 2, &quot;missing&quot; = tagged_na(&quot;x&quot;)))

&lt;labelled&lt;double&gt;[4]&gt;
[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 &lt;- df %&gt;% 
  mutate(x = fct_explicit_na(as_factor(x), na_level = &quot;missing&quot;))

levels(df1$x)

[1] &quot;a&quot;       &quot;b&quot;       &quot;3&quot;       &quot;missing&quot;

huangapple
  • 本文由 发表于 2023年3月9日 18:12:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定