[R] if-else function

Laetitia Schmid laetitia at gmt.su.se
Fri Aug 20 19:53:52 CEST 2010


Hi Richard,
thank you very much. I got the results I needed. But I am still  
interested to find out how it would work with a if-else function in  
this context.
Best,
Laetitia


Am 20.08.2010 um 23:40 schrieb RICHARD M. HEIBERGER:

> > tmp <- "VariablePAR            Plot1            Plot2             
> Plot3            Plot4
> + ParasiteA            3            1            1            4
> + ParasiteB            1            2            3            5
> + ParasiteC            2            1            1            3
> + ParasiteD            2            1            1            4
> + ParasiteE            4            1            1            1"
> > para <- read.table(textConnection(tmp), row.names=1, header=TRUE)
> > para
>           Plot1 Plot2 Plot3 Plot4
> ParasiteA     3     1     1     4
> ParasiteB     1     2     3     5
> ParasiteC     2     1     1     3
> ParasiteD     2     1     1     4
> ParasiteE     4     1     1     1
> > apply(para >= 4, 2, any)
> Plot1 Plot2 Plot3 Plot4
>  TRUE FALSE FALSE  TRUE
> >
> >
> >
> Rich

Hi R people!
I am looking for some suggestions writing an if-else function.
The idea is to characterize different plots containing counts of
variables (here parasites). If a plot has a count equal or higher than
4 for any parasite the function should return a 1 else a 0. Later I
can loop the function over all plots.

Here I have a little subset of my data:

VariablePAR            Plot1            Plot2
Plot3            Plot4
ParasiteA            3            1            1            4
ParasiteB            1            2            3            5
ParasiteC            2            1            1            3
ParasiteD            2            1            1            4
ParasiteE            4            1            1            1

The function should give a 1 for plots 1 and 4 and a 0 for plots 2 and
3.

Your help is very much appreciated,
Laetitia



More information about the R-help mailing list