[R] Subsetting simulation output

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Aug 28 11:44:54 CEST 2001


Murray Jorgensen <maj at waikato.ac.nz> writes:

> Does anyone understand this?
> 
> I have a long vector parlist containing values of 138 parameters in 100
> simulations.
> The factor parno is defined by 
> trys <- 100
> sim <- gl(trys,138)
> parno <- gl(138,1,trys*138)
> 
> I want to extract and compare results for related groups of parameters but I
> get inconsistent results:
> 
> > length(parlist[parno==37:42])
> [1] 600
> > length(parlist[parno==c(37,38,39,40,41,42)])
> [1] 600
> > length(parlist[parno==c(43, 48, 53, 58, 63, 68)])
> [1] 200
> 
> Which I feel should be 600.

You want %in%, not == there.

Beware the effects of vector recycling. Same thing with shorter vectors:

> 1:12 == 3:5
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> 1:12 == 4:6
 [1] FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE

since

> cbind(1:12,3:5,4:6)
      [,1] [,2] [,3]
 [1,]    1    3    4
 [2,]    2    4    5
 [3,]    3    5    6
 [4,]    4    3    4
 [5,]    5    4    5
 [6,]    6    5    6
 [7,]    7    3    4
 [8,]    8    4    5
 [9,]    9    5    6
[10,]   10    3    4
[11,]   11    4    5
[12,]   12    5    6

You're effectively only counting those items for which the number
matches the position modulo 6:

> c(43, 48, 53, 58, 63, 68) %% 6 == 1:6 %% 6
[1]  TRUE FALSE FALSE  TRUE FALSE FALSE
> 37:42 %% 6 == 1:6 %% 6
[1] TRUE TRUE TRUE TRUE TRUE TRUE



-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list