[R] tips for looping over a category for beginner

Ivan Calandra ivan.calandra at uni-hamburg.de
Wed Jan 19 09:45:03 CET 2011


Hi,

If you don't find droplevels(), then you should install the latest 
version of R (2.12.1). It's always a good thing, even more if you're 
starting.

Ivan

Le 1/19/2011 01:27, Ben Harrison a écrit :
> Thanks for the reply Peter.
>
> On 18 January 2011 22:52, Peter Ehlers<ehlers at ucalgary.ca>  wrote:
>
>> Since you don't provide data, let's borrow from the
>> help(droplevels) page:
>>
> I had no joy with my R install finding droplevels exactly, but found this
> instead:
>> ??droplevels
> gdata::drop.levels      Drop unused factor levels
> Is that the same?
>
>
>> aq<- transform(airquality,
>>         Month = factor(Month, labels = month.abb[5:9]))
>>
>> str(aq)
>> #'data.frame':   153 obs. of  6 variables: |
>> # $ Ozone  : int  41 36 12 18 NA 28 23 19  |
>> # $ Solar.R: int  190 118 149 313 NA NA 29 |
>> # $ Wind   : num  7.4 8 12.6 11.5 14.3 14. | etc
>> # $ Temp   : int  67 72 74 62 56 66 65 59  |
>> # $ Month  : Factor w/ 5 levels "May","Jun |
>> # $ Day    : int  1 2 3 4 5 6 7 8 9 10 ... |
>>
>> Now see if the following give you some R inspiration:
>>
>>   plot(Ozone ~ Temp, data = aq)
>>
>> This highlights one of the very confusing aspects of R language for me; is
> plot(x, y) the same as plot (y ~ x)? Seems to be, but maybe I'm missing some
> nuance.
>
>   plot(Ozone ~ Temp, data = aq, subset = {Month == "Sep"})
>>   boxplot(Ozone ~ Month, data = aq)
>>
>>   boxplot(Ozone ~ Month, data = aq,
>>                         subset = {Month != "Aug"})
>>
>>   boxplot(Ozone ~ Month, data = aq,
>>      subset = {!(Month %in% c("Jul", "Aug"))})
>>
>>   boxplot(Ozone ~ Month,
>>      data = droplevels(subset(aq, subset = {Month != "Aug"})))
>>
>>   boxplot(Ozone ~ Month,
>>      data = droplevels(subset(aq, !(Month %in% c("Jul", "Aug")))))
>>
> Thanks for these examples, they mostly make sense!
>
>
>> BTW, attach() is not usually a good idea; have a look at ?with.
>>
>> Great, I thought I had that trick nailed. Obviously there needs to be an R
> equivalent of l2tabu.
>
> Cheers,
> Ben.
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>

-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php



More information about the R-help mailing list