[R] error from character NAs in indexing array
David Winsemius
dwinsemius at comcast.net
Fri Dec 23 15:32:07 CET 2011
The help page for ?Extract says:
"When extracting, a numerical, logical or character NA index picks an
unknown element and so returns NA in the corresponding element of a
logical, integer, numeric, complex or character result, and NULL for a
list. (It returns 00 for a raw result.] "
However, I seem to be getting different behavior when converting NA's
to character indices for an array with character dimnames:
str(frailmed)
num [1:73, 1:2, 1:2] -0.315 -0.31 -0.252 -0.311 -0.29 ...
- attr(*, "dimnames")=List of 3
..$ : chr [1:73] "18" "19" "20" "21" ...
..$ : chr [1:2] "Female" "Male"
..$ : chr [1:2] "FALSE" "TRUE"
> frailmed[as.character(c(18,18,30)), "Female", "TRUE"]
18 18 30
-0.3477757 -0.3477757 -0.3697707
> frailmed[as.character(c(18,18,30,NA)), "Female", "TRUE"]
Error: subscript out of bounds
I started thinking I might need to use grep or match to convert the
numeric value for ageLB to a numeric index for "[", but after looking
at the "manual" is seems I shouldn't be forced to do that.
> as.character(NA)
[1] NA
> frailmed[as.character(NA), "Female", "TRUE"]
Error: subscript out of bounds
I'm guessing that NA_character_ is not being recognized by the
extraction engine:
> frailmed[NA_character_, "Female", "TRUE"]
Error: subscript out of bounds
Test subset:
test <- structure(c(-0.314905119091182, -0.309721294756184,
-0.252154582680236,
+ 0.218589733943967, 0.227724425528112, 0.219274402692938,
-0.347775659359612,
+ -0.351245759870099, -0.361973673972107, 0.155255554082878,
0.149765287705234,
+ 0.150206947093079), .Dim = c(3L, 2L, 2L), .Dimnames = list(c("18",
+ "19", "20"), c("Female", "Male"), c("FALSE", "TRUE")))
> test
, , FALSE
Female Male
18 -0.3149051 0.2185897
19 -0.3097213 0.2277244
20 -0.2521546 0.2192744
, , TRUE
Female Male
18 -0.3477757 0.1552556
19 -0.3512458 0.1497653
20 -0.3619737 0.1502069
> test[NA, 1,1]
<NA> <NA> <NA>
NA NA NA
> test[as.character(NA), 1,1]
Error: subscript out of bounds
Is this intended behavior?
I do see from that last successful execution that I may need to attack
my problem by just processing complete cases, since the return of a
vector of NA's with length > 1 will also cause my logic to fail, but
this behavior seems different than described.
sessionInfo()
R version 2.14.0 Patched (2011-11-13 r57650)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] splines stats graphics grDevices utils datasets
[7] methods base
other attached packages:
[1] rms_3.3-2 Hmisc_3.9-0 survival_2.36-10 sos_1.3-1
[5] brew_1.0-6 lattice_0.20-0
loaded via a namespace (and not attached):
[1] cluster_1.14.1 grid_2.14.0 tools_2.14.0
>
--
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list