[R] Curious subsetting behavior

Liaw, Andy andy_liaw at merck.com
Thu Mar 2 21:25:12 CET 2006


Just because R doesn't print the extraneous ".0" you think the result is
wrong?  

Andy

From: Sean Davis
> 
> I have a simple vector, called tmp that I want to subset 
> based on another vector called vec.  Everything works as 
> expected except for below where the subsetting returns 
> something other than the original data.  Any ideas?
> 
> > vec <- c(1,2,3,4,5,59,60,27,32,21)
> > tmp
>  [1]  1.0  1.1  2.0  2.1  2.2  3.0  3.1  4.0  5.0  5.1  6.0  
> 7.0  8.0  8.1 9.0 [16]  9.1  9.2 10.0 10.1 11.0 12.0 13.0 
> 14.0 15.0 16.0 17.0 18.0 18.1 19.0 20.0 [31] 20.1 21.0 22.0 
> 23.0 24.0 25.0 26.0 27.0 28.0 28.1 29.0 29.1 30.0 31.0 32.0 
> [46] 34.0 35.0 37.0 37.1 38.0 39.0 40.0 41.0 42.0 43.0 44.0 
> 45.0 45.1 46.0 48.0 [61] 50.0
> > tmp[-vec]
>  [1]  3.0  3.1  4.0  5.0  5.1  6.0  7.0  8.0  8.1  9.0  9.1  
> 9.2 10.0 10.1 11.0 [16] 13.0 14.0 15.0 16.0 17.0 18.1 19.0 
> 20.0 20.1 22.0 23.0 24.0 25.0 26.0 27.0 [31] 28.0 28.1 29.0 
> 29.1 30.0 31.0 32.0 34.0 35.0 37.0 37.1 38.0 39.0 40.0 41.0 
> [46] 42.0 43.0 44.0 45.0 45.1 50.0
> > vec <- which(!is.na(MA.exp$targets$Grade))
> > vec
>  [1]  1  3  8  9 11 12 13 15 18 21 22 23 24 25 26 27 29 30 32 
> 33 34 35 36 37 38 [26] 39 41 43 44 45 46 47 48 51 52 53 54 55 
> 56 57 59 60 61
> > str(vec)
>  int [1:43] 1 3 8 9 11 12 13 15 18 21 ...
> > tmp[1:20]
>  [1]  1.0  1.1  2.0  2.1  2.2  3.0  3.1  4.0  5.0  5.1  6.0  
> 7.0  8.0  8.1 9.0 [16]  9.1  9.2 10.0 10.1 11.0
> 
> Everything above is as expected.  However, look at the output 
> of tmp[vec] below.  Why are the values of tmp incorrect?  
> What am I missing?
> 
> > tmp[vec]
>  [1]  1  2  4  5  6  7  8  9 10 12 13 14 15 16 17 18 19 20 21 
> 22 23 24 25 26 27 [26] 28 29 30 31 32 34 35 37 39 40 41 42 43 
> 44 45 46 48 50
> > sessionInfo()
> Version 2.3.0 Under development (unstable) (2006-01-04 
> r36984) powerpc-apple-darwin8.3.0
> 
> attached base packages:
> [1] "tools"     "methods"   "stats"     "graphics"  
> "grDevices" "utils"
> [7] "datasets"  "base"
> 
> other attached packages:
> geneplotter    annotate     Biobase      gplots       gdata   
>    gtools
>     "1.9.5"     "1.9.2"     "1.9.4"     "2.0.0"     "2.0.0"   
>   "2.0.0"
>   RdbiPgSQL   RdbiPgSQL        Rdbi     seanlib       limma
>     "1.0.9"     "1.0.9"     "1.0.4"       "1.0"     "2.4.9"
> 
> 
> Thanks,
> Sean
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list 
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list