r - Assignment to subset of a matrix with repeated indices -
not sure qualifies entry in r-inferno, can comment on logic behind way following replacement works?
foo<-matrix(1:6,2) bar<-foo[2,c(1,3,1)] bar # [1] 2 6 2 foo[2,c(1,3,1)]<-foo[2,c(1,3,1)]+5 foo #      [,1] [,2] [,3] # [1,]    1    3    5 # [2,]    7    4   11   my question is: when generating bar, repeated coordinate results in repeated element in output, when modifying foo, repeated coordinate not result in repeated addition operation.  (by comparison, for(j in c(1,3,1) ) foo[2,j]<-foo[2,j]+5 does).  why & how [<- ignore repeated index?
from help("[<-"):
subassignment done sequentially, if index specified more once latest assigned value index result.
foo<-matrix(1:6,2)  foo[1,rep(1,2)] <- c(1,42)  #     [,1] [,2] [,3] #[1,]   42    3    5 #[2,]    2    4    6      
Comments
Post a Comment