[R] about the subscript assignment porblem

Thomas Lumley tlumley at u.washington.edu
Tue Nov 30 19:56:27 CET 2004

On Tue, 30 Nov 2004, kh zu wrote:

> Hi, Dear,
>  I have a problem for the new version 2.0 of R
> When I put this code in it give me the error:
> uis$ivhx3[uis$ivhx>0] <- 1*(uis$ivhx==3)
> Error: NAs are not allowed in subscripted assignments
> but this would happen in version 1.91

What would happen?  In version 1.9.0 (I don't have a 1.9.1 lying around) 
you get a warning if any of the subscripts are FALSE
   > x<-1:3
   > y<-7:9
   > x[c(TRUE,NA,FALSE)]<-y
   Warning message:
   number of items to replace is not a multiple of replacement length

and if all the subscripts are TRUE or NA the NAs are treated as FALSE

   > x<-1:3
   > y<-7:9
   > x[c(TRUE,NA,TRUE)]<-y
   > x
   [1] 7 2 9

The change was largely motivated by numerical indices, where the problem 
is worse.   In particular if x is 1,2,3, i is 1, NA, 2  and y is 7,8,9, 
    x[i] <- y
set the second element of x to 8 or 9?  The answer was different for 
vectors and matrices in 1.9.1.

It was clearly a deliberate change, not a mistake (it's hard to see how it 
could have happened accidentally, and it was documented), and the previous 
behaviour was wrong.


More information about the R-help mailing list