我有兴趣指定缺失值的类型。我有不同类型的丢失的数据,我试图将这些值编码为R中缺少的,但我正在寻找一个解决方案,我仍然可以区分它们。
说我有一些看起来像这样的数据,
set.seed(667)
df <- data.frame(a = sample(c("Don't know/Not sure","Unknown","Refused","Blue", "Red", "Green"), 20, rep=TRUE), b = sample(c(1, 2, 3, 77, 88, 99), 10, rep=TRUE), f = round(rnorm(n=10, mean=.90, sd=.08), digits = 2), g = sample(c("C","M","Y","K"), 10, rep=TRUE) ); df
# a b f g
# 1 Unknown 2 0.78 M
# 2 Refused 2 0.87 M
# 3 Red 77 0.82 Y
# 4 Red 99 0.78 Y
# 5 Green 77 0.97 M
# 6 Green 3 0.99 K
# 7 Red 3 0.99 Y
# 8 Green 88 0.84 C
# 9 Unknown 99 1.08 M
# 10 Refused 99 0.81 C
# 11 Blue 2 0.78 M
# 12 Green 2 0.87 M
# 13 Blue 77 0.82 Y
# 14 Don't know/Not sure 99 0.78 Y
# 15 Unknown 77 0.97 M
# 16 Refused 3 0.99 K
# 17 Blue 3 0.99 Y
# 18 Green 88 0.84 C
# 19 Refused 99 1.08 M
# 20 Red 99 0.81 C
如果我现在制作两个表格,我的缺失值("Don't know/Not sure","Unknown","Refused"
和77, 88, 99
)将作为常规数据包含在内,
table(df$a,df$g)
# C K M Y
# Blue 0 0 1 2
# Don't know/Not sure 0 0 0 1
# Green 2 1 2 0
# Red 1 0 0 3
# Refused 1 1 2 0
# Unknown 0 0 3 0
和
table(df$b,df$g)
# C K M Y
# 2 0 0 4 0
# 3 0 2 0 2
# 77 0 0 2 2
# 88 2 0 0 0
# 99 2 0 2 2
我现在将三个因素级别"Don't know/Not sure","Unknown","Refused"
重新编码为<NA>
is.na(df[,c("a")]) <- df[,c("a")]=="Don't know/Not sure"|df[,c("a")]=="Unknown"|df[,c("a")]=="Refused"
并删除空白级别
df$a <- factor(df$a)
并使用数值77, 88,
和99
is.na(df) <- df=="77"|df=="88"|df=="99"
table(df$a, df$g, useNA = "always")
# C K M Y <NA>
# Blue 0 0 1 2 0
# Green 2 1 2 0 0
# Red 1 0 0 3 0
# <NA> 1 1 5 1 0
table(df$b,df$g, useNA = "always")
# C K M Y <NA>
# 2 0 0 4 0 0
# 3 0 2 0 2 0
# <NA> 4 0 4 4 0
现在,丢失的类别将重新编码为NA
,但它们都被整合在一起。是否有一种方法可以将某些内容重新编码为缺失,但保留原始值?我希望R将"Don't know/Not sure","Unknown","Refused"
和77, 88, 99
作为缺失线程,但我希望能够在变量中保留信息。
答案 0 :(得分:19)
据我所知,base R没有内置的方法来处理不同的NA
类型。 (编辑:它确实:NA_integer_
,NA_real_
,NA_complex_
和NA_character
。请参阅?base::NA
。< / em>的
一种选择是使用这样做的包,例如“memisc”。这是一些额外的工作,但它似乎做你想要的。
以下是一个例子:
首先,您的数据。我已经制作了一份副本,因为我们将对数据集进行一些非常重要的更改,并且备份总是很好。
set.seed(667)
df <- data.frame(a = sample(c("Don't know/Not sure", "Unknown",
"Refused", "Blue", "Red", "Green"),
20, replace = TRUE),
b = sample(c(1, 2, 3, 77, 88, 99), 10,
replace = TRUE),
f = round(rnorm(n = 10, mean = .90, sd = .08),
digits = 2),
g = sample(c("C", "M", "Y", "K"), 10,
replace = TRUE))
df2 <- df
让因子变量“a”:
df2$a <- factor(df2$a,
levels = c("Blue", "Red", "Green",
"Don't know/Not sure",
"Refused", "Unknown"),
labels = c(1, 2, 3, 77, 88, 99))
加载“memisc”库:
library(memisc)
现在,将变量“a”和“b”转换为“memisc”中的item
:
df2$a <- as.item(as.character(df2$a),
labels = structure(c(1, 2, 3, 77, 88, 99),
names = c("Blue", "Red", "Green",
"Don't know/Not sure",
"Refused", "Unknown")),
missing.values = c(77, 88, 99))
df2$b <- as.item(df2$b,
labels = c(1, 2, 3, 77, 88, 99),
missing.values = c(77, 88, 99))
通过这样做,我们有了一种新的数据类型。比较以下内容:
as.factor(df2$a)
# [1] <NA> <NA> Red Red Green Green Red Green <NA> <NA> Blue
# [12] Green Blue <NA> <NA> <NA> Blue Green <NA> Red
# Levels: Blue Red Green
as.factor(include.missings(df2$a))
# [1] *Unknown *Refused Red
# [4] Red Green Green
# [7] Red Green *Unknown
# [10] *Refused Blue Green
# [13] Blue *Don't know/Not sure *Unknown
# [16] *Refused Blue Green
# [19] *Refused Red
# Levels: Blue Red Green *Don't know/Not sure *Refused *Unknown
我们可以使用此信息创建以您描述的方式运行的表,同时保留所有原始信息。
table(as.factor(include.missings(df2$a)), df2$g)
#
# C K M Y
# Blue 0 0 1 2
# Red 1 0 0 3
# Green 2 1 2 0
# *Don't know/Not sure 0 0 0 1
# *Refused 1 1 2 0
# *Unknown 0 0 3 0
table(as.factor(df2$a), df2$g)
#
# C K M Y
# Blue 0 0 1 2
# Red 1 0 0 3
# Green 2 1 2 0
table(as.factor(df2$a), df2$g, useNA="always")
#
# C K M Y <NA>
# Blue 0 0 1 2 0
# Red 1 0 0 3 0
# Green 2 1 2 0 0
# <NA> 1 1 5 1 0
具有缺失数据的数字列的表行为相同。
table(as.factor(include.missings(df2$b)), df2$g)
#
# C K M Y
# 1 0 0 0 0
# 2 0 0 4 0
# 3 0 2 0 2
# *77 0 0 2 2
# *88 2 0 0 0
# *99 2 0 2 2
table(as.factor(df2$b), df2$g, useNA="always")
#
# C K M Y <NA>
# 1 0 0 0 0 0
# 2 0 0 4 0 0
# 3 0 2 0 2 0
# <NA> 4 0 4 4 0
作为奖励,您可以获得良好的codebook
s:
> codebook(df2$a)
========================================================================
df2$a
------------------------------------------------------------------------
Storage mode: character
Measurement: nominal
Missing values: 77, 88, 99
Values and labels N Percent
1 'Blue' 3 25.0 15.0
2 'Red' 4 33.3 20.0
3 'Green' 5 41.7 25.0
77 M 'Don't know/Not sure' 1 5.0
88 M 'Refused' 4 20.0
99 M 'Unknown' 3 15.0
但是,我建议您阅读@ Maxim.K中的the comment,了解真正构成缺失值的内容。
答案 1 :(得分:5)
要保留原始值,您可以创建用于编码NA信息的新列,例如:
df <- transform(df,b.na = ifelse(b %in% c('77','88','99'),NA,b))
df <- transform(df,a.na = ifelse(a %in%
c("Don't know/Not sure","Unknown","Refused"),NA,a))
然后你可以这样做:
table(df$b.na , df$g)
C K M Y
2 0 0 4 0
3 0 2 0 2
不创建新列的另一个选项是使用这样的exclude
选项将非所需值设置为NULL(不同的缺失值)
table(df$a,df$g,
exclude=c('77','88','99',"Don't know/Not sure","Unknown","Refused"))
C K M Y
Blue 0 0 1 2
Green 2 1 2 0
Red 1 0 0 3
您可以定义一些全局常量(即使不建议)将您的“缺失值”分组,并在程序的其余部分中使用它们。像这样:
B_MISSING <- c('77','88','99')
A_MISSING <- c("Don't know/Not sure","Unknown","Refused")
答案 2 :(得分:4)
如果您愿意坚持数值,那么NA
,Inf
,-Inf
和NaN
可用于不同的缺失值。然后,您可以使用is.finite
来区分它们和正常值:
> x <- c(NA, Inf, -Inf, NaN, 1)
> is.finite(x)
[1] FALSE FALSE FALSE FALSE TRUE
你可以有一个特殊的打印功能,以更有意义的方式显示它们,甚至可以创建一个特殊的类,但即使没有它,这也会将数据分成有限的和多个非有限的值。