當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。