If you index a vector with a vector that has NA in it, you get NA back:
> x=101:107
> x[c(NA,4,NA)]
[1] NA 104 NA
> x[c(4,NA)]
[1] 104 NA
All well and good. ?"[" says, under NAs in indexing:
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.]
But if the indexing vector is all NA, you get back a vector of length
of your original vector rather than of your index vector:
> x[c(NA,NA)]
[1] NA NA NA NA NA NA NA
Maybe it's just me, but I find this surprising, and I can't see it
documented. Bug or undocumented feature? Apologies if I've missed
something obvious.
Barry
sessionInfo()
R version 2.11.0 alpha (2010-03-25 r51407)
i686-pc-linux-gnu
locale:
[1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8
[5] LC_MONETARY=C LC_MESSAGES=en_GB.UTF-8
[7] LC_PAPER=en_GB.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
Is this, from the man page, relevant?
"An empty index selects all values: this is most often used to replace all
the entries but keep the attributes. "
Bert Gunter
Genentech Nonclinical Statistics
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On
Behalf Of Barry Rowlingson
Sent: Friday, March 26, 2010 5:10 AM
To: r-help at r-project.org
Subject: [R] NA values in indexing
If you index a vector with a vector that has NA in it, you get NA back:
> x=101:107
> x[c(NA,4,NA)]
[1] NA 104 NA
> x[c(4,NA)]
[1] 104 NA
All well and good. ?"[" says, under NAs in indexing:
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.]
But if the indexing vector is all NA, you get back a vector of length
of your original vector rather than of your index vector:
> x[c(NA,NA)]
[1] NA NA NA NA NA NA NA
Maybe it's just me, but I find this surprising, and I can't see it
documented. Bug or undocumented feature? Apologies if I've missed
something obvious.
Barry
sessionInfo()
R version 2.11.0 alpha (2010-03-25 r51407)
i686-pc-linux-gnu
locale:
[1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8
[5] LC_MONETARY=C LC_MESSAGES=en_GB.UTF-8
[7] LC_PAPER=en_GB.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
The type of 'NA' is logical. So x[NA] behaves more like x[TRUE] i.e. silent recycling.> class(NA)[1] "logical"> x=101:108 > x[NA][1] NA NA NA NA NA NA NA NA> x[c(TRUE,NA)][1] 101 NA 103 NA 105 NA 107 NA> x[as.integer(NA)][1] NA HTH Matthew "Barry Rowlingson" <b.rowlingson at lancaster.ac.uk> wrote in message news:d8ad40b51003260509y6b671e53o9f79142d2b52cf37 at mail.gmail.com... If you index a vector with a vector that has NA in it, you get NA back: > x=101:107 > x[c(NA,4,NA)] [1] NA 104 NA > x[c(4,NA)] [1] 104 NA All well and good. ?"[" says, under NAs in indexing: 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.] But if the indexing vector is all NA, you get back a vector of length of your original vector rather than of your index vector: > x[c(NA,NA)] [1] NA NA NA NA NA NA NA Maybe it's just me, but I find this surprising, and I can't see it documented. Bug or undocumented feature? Apologies if I've missed something obvious. Barry sessionInfo() R version 2.11.0 alpha (2010-03-25 r51407) i686-pc-linux-gnu locale: [1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8 [5] LC_MONETARY=C LC_MESSAGES=en_GB.UTF-8 [7] LC_PAPER=en_GB.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base
Try> x <- 101:107 > x[c(NA_integer_, NA_integer_)][1] NA NA On Fri, Mar 26, 2010 at 8:09 AM, Barry Rowlingson <b.rowlingson at lancaster.ac.uk> wrote:> If you index a vector with a vector that has NA in it, you get NA back: > > ?> x=101:107 > ?> x[c(NA,4,NA)] > ?[1] ?NA 104 ?NA > ?> x[c(4,NA)] > ?[1] 104 ?NA > > All well and good. ?"[" says, under NAs in indexing: > > ? ? 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.] > > But if the indexing vector is all NA, you get back a vector of length > of your original vector rather than of your index vector: > > ?> x[c(NA,NA)] > ?[1] NA NA NA NA NA NA NA > > Maybe it's just me, but I find this surprising, and I can't see it > documented. Bug or undocumented feature? Apologies if I've missed > something obvious. > > Barry > > ?sessionInfo() > R version 2.11.0 alpha (2010-03-25 r51407) > i686-pc-linux-gnu > > locale: > ?[1] LC_CTYPE=en_GB.UTF-8 ? ? ? LC_NUMERIC=C > ?[3] LC_TIME=en_GB.UTF-8 ? ? ? ?LC_COLLATE=en_GB.UTF-8 > ?[5] LC_MONETARY=C ? ? ? ? ? ? ?LC_MESSAGES=en_GB.UTF-8 > ?[7] LC_PAPER=en_GB.UTF-8 ? ? ? LC_NAME=C > ?[9] LC_ADDRESS=C ? ? ? ? ? ? ? LC_TELEPHONE=C > [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C > > attached base packages: > [1] stats ? ? graphics ?grDevices utils ? ? datasets ?methods ? base > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >