[R] tips for looping over a category for beginner

Ivan Calandra ivan.calandra at uni-hamburg.de
Tue Jan 18 09:34:56 CET 2011


Hi,

First a good tip when you ask on the R list is to provide data in a way 
that we can readily use it. I think the best way to do it is to copy the 
output of dput(tc) into the email you write.

There might be better ways to do what you want, but here is what I would do:

#first subset what you need:
tc_clay <- tc[LithClass=="clay",]
#or
tc_not_clay <- tc[LithClass!="clay",]

#you might need to drop the unused levels if you don't want all of them 
to be plotted without values. Two ways (at least) to do it:
tc_clay <- factor(tc_clay)
tc_not_clay <- droplevels(tc_not_clay)

#then you can plot
plot(tc_clay$Conductivity~tc_clay$Depth)
boxplot(tc_not_clay$Conductivity~tc_not_clay$LithClass)

I hope it will help you get started.
Ivan

Le 1/18/2011 07:48, Ben Harrison a écrit :
> hello, I am very new to R.
> My current data set is a mix of values and categories. It is a geoscience
> data set, with values per rock sample. Case in point, each sample belongs to
> a lithology class, and each sample has several physical property
> measurements (density, porosity...).
>
> I want to be able to plot these physical properties for all samples in each
> lithology class. this is how i'm doing it now:
>
>> tc = read.table(....)
>> attach(tc)
>> names(tc)
> tc = [1] "Well"            "Depth"           "Latitude"
> "Longitude"
>   [5] "Formation"       "Lithology"       "LithClass"       "CondUncert"
>   [9] "sample.ID" "Conductivity"    "Density"         "Porosity"
>
>> plot(Depth[LithClass=='sand'], Conductivity[LithClass=='sand'])
> (ad nauseum... how can I loop through them all?)
>
> and ...
>
>> boxplot(Conductivity[LithClass=='clay'])
>> boxplot(Conductivity~LithClass)    #  whole set of boxplots on one
> diagram, but
>      # what if want to exclude one or two of the LithClasses?
>
> and ...
>
>> pairs(c(tc[10],tc[2],tc[11],tc[12]))
> this is as advanced as I've got.
>
> Any tips would be greatly appreciated.
>
> 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