NA 레벨에 의한 요인을 하위 집합하십시오
-
14-11-2019 - |
문제
R은 R에 계수가 있습니다.
set.seed(1)
x <- sample(c(1, 2, NA), 25, replace=TRUE)
x <- factor(x, exclude = NULL)
> x
[1] 1 2 2 <NA> 1 <NA> <NA> 2 2 1 1
[12] 1 <NA> 2 <NA> 2 <NA> <NA> 2 <NA> <NA> 1
[23] 2 1 1
Levels: 1 2 <NA>
.
<NA>
레벨에 의해 그 요인을 어떻게 하위 집합합니까?내가 시도한 두 가지 방법은 작동하지 않았습니다.
> x[is.na(x)]
factor(0)
Levels: 1 2 <NA>
> x[x=='<NA>']
factor(0)
Levels: 1 2 <NA>
. 해결책
Surprising to me that your attempts to do this didn't work, but this seems to:
x[is.na(levels(x)[x])]
I got there by looking at str(x)
and seeing that it is the levels that are NA
, not the underlying codes:
str(x)
Factor w/ 3 levels "1","2",NA: 1 2 2 3 1 3 3 2 2 1 ...
다른 팁
As a follow up to Ben:
str(x)
shows you the problem. Factors are stored as integers internally with a "lookup" of sorts. So:
> all(is.na(x))
[1] FALSE
but
> any(is.na(levels(x)))
[1] TRUE
and as ben showed, to print the actual values of the vector:
> levels(x)[x]
[1] "1" "2" "2" NA "1" NA NA "2" "2" "1" "1" "1" NA "2" NA "2" NA NA "2" NA NA "1" "2" "1" "1"
versus
> x
[1] 1 2 2 <NA> 1 <NA> <NA> 2 2 1 1 1 <NA> 2 <NA> 2 <NA> <NA> 2 <NA> <NA> 1 2 1 1
Levels: 1 2 <NA>
제휴하지 않습니다 StackOverflow