Re: [R] If statements for vectors

From: Vincent Goulet <vincent.goulet_at_act.ulaval.ca>
Date: Wed, 09 Apr 2008 20:34:55 -0400

Le mer. 9 avr. à 18:00, Paul Johnson a écrit :
> On Wed, Apr 9, 2008 at 4:07 PM, Laura Bonnett <L.J.Bonnett@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@act.ulaval.ca   http://vgoulet.act.ulaval.ca

______________________________________________
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.
Received on Thu 10 Apr 2008 - 00:42:11 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 10 Apr 2008 - 01:30:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive