An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110118/3daa9544/attachment.pl>
tips for looping over a category for beginner
10 messages · Ivan Calandra, Dennis Murphy, Ben Harrison +2 more
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110118/befdba70/attachment.pl>
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110119/a5aa12d8/attachment.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110119/500011da/attachment.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110119/5c0b5ff6/attachment.pl>
On Jan 18, 2011, at 7:32 PM, Ben Harrison wrote:
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:
As an aside, is it normal practice then to attach data files to questions on this mailing list? I might do that in future if it's possible and acceptable.
PLEASE. (re?-)read the Posting Guide.
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.
David Winsemius, MD West Hartford, CT
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
On 2011-01-18 16:27, Ben Harrison wrote:
[...snip...]
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 has many methods; see with methods(plot). If you go to the help page for plot you'll be pointed to plot.default and plot.formula (under 'See Also'). Have a look at those to see what the difference is. The formula method is useful in that it permits you to set the 'data' argument (which obviates the need for 'attach'ing. [...snip...]
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.
I wouldn't call it a sin, but I find alternatives like with() and 'data=' much more convenient. Peter Ehlers