[R] tips for looping over a category for beginner

Peter Ehlers ehlers at ucalgary.ca
Tue Jan 18 12:52:40 CET 2011


On 2011-01-17 22:48, Ben Harrison wrote:
> 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.
>

Since you don't provide data, let's borrow from the
help(droplevels) page:

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)

  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")))))

BTW, attach() is not usually a good idea; have a look at ?with.

Peter Ehlers



More information about the R-help mailing list