[R] NA values in indexing

2010-03-26 Thread Barry Rowlingson
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

Re: [R] NA values in indexing

2010-03-26 Thread Matthew Dowle
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

Re: [R] NA values in indexing

2010-03-26 Thread Gabor Grothendieck
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

Re: [R] NA values in indexing

2010-03-26 Thread Bert Gunter
] 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

Re: [R] NA values in indexing

2010-03-26 Thread Barry Rowlingson
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:

Re: [R] NA values in indexing

2010-03-26 Thread Barry Rowlingson
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