[R] If statements for vectors

Vincent Goulet vincent.goulet at act.ulaval.ca
Thu Apr 10 02:34:55 CEST 2008


Le mer. 9 avr. à 18:00, Paul Johnson a écrit :
> On Wed, Apr 9, 2008 at 4:07 PM, Laura Bonnett <L.J.Bonnett at warwick.ac.uk 
> > wrote:
>> Dear Sirs,
>>
>> I am using both the Bioconductor adds on (Affy, AffyPLM,...) and the
>> 'standard' R-package.
>>
>> I am trying to select a list of genes which all have expression  
>> values below
>> a certain threshold.
>> I have done this by creating a vector which has 0s where the  
>> expression is
>> greater than the threshold and 1s where it is less than or equal to  
>> it.
>> Multiplying this vector by the expression values produces a list of  
>> 0s and
>> expression values below the threshold value.
>>
>> However, now I need to remove the 0s.  I thought that this would be
>> relatively trivial but it appears it isn't!!!
>>
>
> Without a working example from you, I have no way to test this
> proposal.  But if I were you, I would get the index values of the
> right cases in one step, and then use that to choose the ones I want.
>
> theGoodOnes <- which(exp2 >= 0)
> exp3 <- exp2[theGoodOnes]
>
>
> This can be crammed into one line, but I'd do it in two just to make
> sure it is correct, at least the first time.

This type of extraction is so common that it has become idiomatic to  
write

exp3 <- exp2[exp2 >= 0]

Also, Laura, if you want to truncate (not eliminate) values to a  
certain threshold or limit, use the vectorized functions pmax() and  
pmin(). For the type of manipulation you needed to do it's almost  
never necessary to use for() loops.

HTH

Vincent

>
>
> My example:
>
>> x <- rnorm(100)
>> which(x >= 0)
> [1]  9 11 12 13 14 16 19 20 21 22 24 25 28 30 32 34 35 36 38 40 41  
> 42 43 44 46
> [26] 47 49 50 53 54 57 59 61 63 64 66 68 69 70 72 75 78 80 81 82 83  
> 88 90 97 98
>> theGoodOnes <- which(x>=0)
>> newX <- x[theGoodOnes]
>> newX
> [1] 0.89285908 0.51753998 1.18485887 2.10003705 0.54535841 1.28313738
> [7] 1.34092172 0.76064356 0.02201121 0.80808363 0.04578730 0.23045983
> [13] 1.04306626 0.12694184 0.89706863 0.86302992 1.53471660 0.51192410
> [19] 1.00366834 1.76923470 0.49508470 1.27888454 0.76706729 1.46340483
> [25] 1.69315538 0.50537603 0.18422329 0.72968629 0.45490526 2.18208183
> [31] 0.71926926 0.68915832 1.49076770 0.48763971 0.39273110 0.80709549
> [37] 0.22099019 0.38103757 0.14626929 0.63933750 1.26643194 3.33091910
> [43] 2.50341609 2.05286611 0.31986095 0.64548972 0.34712937 0.04075135
> [49] 0.07206342 0.20325505
>
>
> -- 
> Paul E. Johnson
> Professor, Political Science
> 1541 Lilac Lane, Room 504
> University of Kansas
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

---
   Vincent Goulet, Associate Professor
   École d'actuariat
   Université Laval, Québec
   Vincent.Goulet at act.ulaval.ca   http://vgoulet.act.ulaval.ca



More information about the R-help mailing list