Re: [R] [PS] Re: a more elegant way to get percentages? (now R books)

2008-03-13 Thread Ben Fairbank
Monica --

There has been a virtual population explosion of R books in recent years
and we all have our favorites.  You may wish to pick one oriented toward
your specialty, but the absolute minimum lowest common denominator (by
which I mean that it has the ground zero essential information that all
users must share, not that it is minimal or incomplete) is the manual
An Introduction to R, available by download from the Cran website.
Beyond that, my favorite introduction is Peter Dalgaard's Introductory
Statistics with R.  He has an elegance and clarity of style, as well as
a feel for what is necessary to include in an introduction, that some
others lack.  Others may disagree, but I find myself returning to
Dalgaard again and again. 

Ben 

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
On Behalf Of Monica Pisica
Sent: Thursday, March 13, 2008 9:05 AM
To: Gabor Grothendieck; [EMAIL PROTECTED];
[EMAIL PROTECTED]
Cc: r-help@r-project.org
Subject: [PS] Re: [R] a more elegant way to get percentages?



Hi everybody,


I am amazed how quick i got my answer ;-) I have to recognize that
Gabor's code really puts to shame my skills in doing any programming in
R. Is there any book or documentation which really explains in details
all these neat tricks from {stats} like ave (i even didn't know this
function existed), apply and all its friends (sapply, tapply, etc) ? To
be honest it took me quite a while to come up with the fancy
subscripting to get my persantages ;-))

thank you so much, i really appreciate your help,

Monica

 Date: Thu, 13 Mar 2008 09:45:05 -0400
 From: [EMAIL PROTECTED]
 To: [EMAIL PROTECTED]
 Subject: Re: [R] a more elegant way to get percentages?
 CC: r-help@r-project.org

 Assuming your x is as follows:

 x - data.frame(locat = c(a, b, b, c, c, c, c, d, d,
d),
 val = c(5, 5, 15, 5, 20, 5, 10, 5, 15, 10))

 Try this:

 x$percent1 - ave(x$val, x$locat, FUN = function(x) 100*x/sum(x))

 On Thu, Mar 13, 2008 at 9:36 AM, Monica Pisica  wrote:

 Hi,

 I am trying to get percentages in a more elegant way. I have a
data.frame with locations and values (counts) of species at that
location. Each location is repeated for each species i have values for
and i would like to get percentages of each species at that location. I
am not sure if i am clear in my explanations so i will paste my code
below:

 #

 x
 locat val
 1 a 5
 2 b 5
 3 b 15
 4 c 5
 5 c 20
 6 c 5
 7 c 10
 8 d 5
 9 d 15
 10 d 10
 loc1 - x$locat
 n - length(loc1)
 locuniq1 - unique(loc1)
 m - length(locuniq1)
 counts - seq(1:m)

 for (i in 1:m) {
 + count - 0
 + for (j in 1:n) {
 + if (loc1[j]==locuniq1[i]) count - count+1
 + counts[i] - count
 + }
 + }

 percent1 - rep(0,n)
 j - 0
 for (i in 1:m) {
 +
 + b - x[(j+1):(j+counts[i]),]
 + total - sum(b$val)
 + percent1[(j+1):(j+counts[i])] - round(apply(as.matrix(b$val), 1,
function(x) {x*100/total}),2)
 + j = j+counts[i]
 + }
 x1 - cbind(x, percent1) # this is the result i want
 x1
 locat val percent1
 1 a 5 100.00
 2 b 5 25.00
 3 b 15 75.00
 4 c 5 12.50
 5 c 20 50.00
 6 c 5 12.50
 7 c 10 25.00
 8 d 5 16.67
 9 d 15 50.00
 10 d 10 33.33

 

 I am wondering if there is any way to do it more efficiently, much
more that the first loop which gives how many times each location is
present in the data.frame is slow if you have a larger data.frame and
not only 10 rows.

 Thanks for any input and sorry if the email is on the long side,

 Monica


 _
 [[elided Hotmail spam]]

 __
 R-help@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.


_


08
__
R-help@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.

__
R-help@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.


Re: [R] [PS] Re: a more elegant way to get percentages? (now R books)

2008-03-13 Thread hadley wickham
  There has been a virtual population explosion of R books in recent years
  and we all have our favorites.  You may wish to pick one oriented toward
  your specialty, but the absolute minimum lowest common denominator (by
  which I mean that it has the ground zero essential information that all
  users must share, not that it is minimal or incomplete) is the manual
  An Introduction to R, available by download from the Cran website.

I don't mean to pick on you in particular, or on the authors of An
introduction to R, but I really don't see how anyone in good
conscience can recommend this to a new user of R.  I think it does a
great job of covering the basics, and is probably a good read after
you've been using R for a year or so, but in goes into a lot of depth
into things that you really don't need to know for doing practical,
day-to-day data analysis.

For example, you don't find out how to actually load data into R until
page 30, while you get 3 page on the mode and length of objects at
page 12.  Do we really need to know that an empty (zero length) vector
still has a mode?

These comments are based on my experience teaching R to undergrad stat
majors, and so may not apply to your audience.  If you teach R in the
same order as an introduction to R it takes you about 4 weeks before
you can actually do anything useful with R, by which time the students
are bored to tears.  If you start with getting data into R and
displaying the data with graphics, you can do useful things very
quickly, providing interest and motivation, and then you can gradually
introduce a more rigourous description of the components as needed.

Hadley

-- 
http://had.co.nz/

__
R-help@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.