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
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.rowling...@lancaster.ac.uk
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.rowling...@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
] On
Behalf Of Barry Rowlingson
Sent: Friday, March 26, 2010 5:10 AM
To: r-help@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
On Fri, Mar 26, 2010 at 4:15 PM, Bert Gunter gunter.ber...@gene.com wrote:
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.
No, I think that means doing x[], and only in replacement:
On Fri, Mar 26, 2010 at 4:15 PM, Matthew Dowle mdo...@mdowle.plus.com wrote:
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
6 matches
Mail list logo