[R] if-else function

David Winsemius dwinsemius at comcast.net
Sat Aug 21 16:23:00 CEST 2010


On Aug 20, 2010, at 1:53 PM, Laetitia Schmid wrote:

> 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.

lapply(para, function(x) if (max(x) >= 4  ){1} else {0})

-- 
David
> 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
>
> ______________________________________________
> 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.



More information about the R-help mailing list