当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R haven tagged_na “标记”缺失值


"Tagged" 缺失值的工作方式与常规 R 缺失值完全相同,只是它们在标签中存储一个额外的信息字节,通常是一个字母("a" 到 "z")。加载 SAS 和 Stata 文件时,标记的缺失值始终使用小写值。

用法

tagged_na(...)

na_tag(x)

is_tagged_na(x, tag = NULL)

format_tagged_na(x, digits = getOption("digits"))

print_tagged_na(x, digits = getOption("digits"))

参数

...

包含单个字符的向量。该字母将用于 "tag" 缺失值。

x

数值向量

tag

如果是 NULL ,则仅当标签具有此值时才返回 true。

digits

字符串表示中使用的位数

细节

format_tagged_na()print_tagged_na() 将标记 NA 的格式设置为 NA(a)、NA(b) 等。

例子

x <- c(1:5, tagged_na("a"), tagged_na("z"), NA)

# Tagged NA's work identically to regular NAs
x
#> [1]  1  2  3  4  5 NA NA NA
is.na(x)
#> [1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE

# To see that they're special, you need to use na_tag(),
# is_tagged_na(), or print_tagged_na():
is_tagged_na(x)
#> [1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE
na_tag(x)
#> [1] NA  NA  NA  NA  NA  "a" "z" NA 
print_tagged_na(x)
#> [1]     1     2     3     4     5 NA(a) NA(z)    NA

# You can test for specific tagged NAs with the second argument
is_tagged_na(x, "a")
#> [1] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE

# Because the support for tagged's NAs is somewhat tagged on to R,
# the left-most NA will tend to be preserved in arithmetic operations.
na_tag(tagged_na("a") + tagged_na("z"))
#> [1] "a"
源代码:R/tagged_na.R

相关用法


注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 "Tagged" missing values。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。