[R] trying ti use a function in aggregate

arun smartpink111 at yahoo.com
Thu Oct 25 20:49:37 CEST 2012


Hi,
Try this:
dat1$Percent<-unlist(tapply(dat1$Count,list(dat1$Trip_id,dat1$CommonName),function(x) x/sum(x)))
head(dat1)
#  Trip_id   Vessel   CommonName Length Count    Percent
#1     230 Sunlight ShadAmerican     19     1 0.01408451
#2     230 Sunlight ShadAmerican     20     1 0.01408451
#3     230 Sunlight ShadAmerican     21     1 0.02816901
#4     230 Sunlight ShadAmerican     23     1 0.05633803
#5     230 Sunlight ShadAmerican     26     1 0.22535211
#6     230 Sunlight ShadAmerican     27     1 0.30985915

I also have a doubt from your first email. 

 "I am trying to use the aggregate function to
calculate the percent at length for each Trip_id and CommonName. "

So, I am thinking you need the percentage of "length".  If that is the case,
res1<-with(dat1,aggregate(Count,by=list(Trip_id=Trip_id,CommonName=CommonName),function(x) length(x)))
library(plyr) 
res1$Percent<-ddply(res1,.(Trip_id),summarize,x/sum(x))[,2]
 res1
#  Trip_id      CommonName  x Percent
#1     230         Alewife 11    0.44
#2     230 HerringBlueback  4    0.16
#3     230    ShadAmerican 10    0.40
#4     231    ShadAmerican  5    1.00
 A.K.






________________________________
From: Sally A Roman <sroman at umassd.edu>
To: arun <smartpink111 at yahoo.com> 
Sent: Thursday, October 25, 2012 12:44 PM
Subject: Re: [R] trying ti use a function in aggregate


Thank you for your help.  Your code does what I need it to, but the output that I need is
Trip_id, Vessel, CommonName, Length, Percent.  When I run your functions there is no length output.  Do you have any suggestions on how to get the Length variable into the output?
Thanks again- Sally

________________________________

From: "arun" <smartpink111 at yahoo.com>
To: "Sally_roman" <sroman at umassd.edu>
Cc: "R help" <r-help at r-project.org>
Sent: Thursday, October 25, 2012 12:04:23 PM
Subject: Re: [R] trying ti use a function in aggregate

Hi,
May be this helps:
dat1<-read.table(text="
 Trip_id          Vessel      CommonName Length Count
1      230        Sunlight    ShadAmerican    19    1
2      230        Sunlight    ShadAmerican    20    1
3      230        Sunlight    ShadAmerican    21    1
4      230        Sunlight    ShadAmerican    23    1
5      230        Sunlight    ShadAmerican    26    1
6      230        Sunlight    ShadAmerican    27    1
7      230        Sunlight    ShadAmerican    30    2
8      230        Sunlight    ShadAmerican    33    1
9      230        Sunlight    ShadAmerican    34    1
10    230        Sunlight    ShadAmerican    37    1
11    230        Sunlight HerringBlueback    20    1
12    230        Sunlight HerringBlueback    21    2
13    230        Sunlight HerringBlueback    22    5
14    230        Sunlight HerringBlueback    26    1
15    230        Sunlight          Alewife    17    1
16    230        Sunlight          Alewife    18    1
17    230        Sunlight          Alewife    20    2
18    230        Sunlight          Alewife    21    4
19    230        Sunlight          Alewife    22    16
20    230        Sunlight          Alewife    23    22
21    230        Sunlight          Alewife    24    16
22    230        Sunlight          Alewife    25    4
23    230        Sunlight          Alewife    26    1
24    230        Sunlight          Alewife    27    2
25    230        Sunlight          Alewife    28    2
26    231 Western_Venture    ShadAmerican    23    1
27    231 Western_Venture    ShadAmerican    24    1
28    231 Western_Venture    ShadAmerican    25    1
29    231 Western_Venture    ShadAmerican    28    2
30    231 Western_Venture    ShadAmerican    29    2
",sep="",header=TRUE,stringsAsFactors=FALSE)

with(dat1,aggregate(Count,by=list(Trip_id=Trip_id,Species=CommonName),function(x) x/sum(x)))
  Trip_id         Species
1     230         Alewife
2     230 HerringBlueback
3     230    ShadAmerican
4     231    ShadAmerican
                                                                                                                                   x
#1 0.01408451, 0.01408451, 0.02816901, 0.05633803, 0.22535211, 0.30985915, 0.22535211, 0.05633803, 0.01408451, 0.02816901, 0.02816901
#2                                                                                         0.1111111, 0.2222222, 0.5555556, 0.1111111
#3             0.09090909, 0.09090909, 0.09090909, 0.09090909, 0.09090909, 0.09090909, 0.18181818, 0.09090909, 0.09090909, 0.09090909
#4                                                                              0.1428571, 0.1428571, 0.1428571, 0.2857143, 0.2857143
#or 
library(plyr)
res<-ddply(dat1,.(Trip_id=Trip_id,Vessel=Vessel,CommonName=CommonName), summarize, Count/sum(Count))
colnames(res)[4]<-"value"
head(res)
#  Trip_id   Vessel CommonName      value
#1     230 Sunlight    Alewife 0.01408451
#2     230 Sunlight    Alewife 0.01408451
#3     230 Sunlight    Alewife 0.02816901
#4     230 Sunlight    Alewife 0.05633803
#5     230 Sunlight    Alewife 0.22535211
#6     230 Sunlight    Alewife 0.30985915


A.K.






----- Original Message -----
From: Sally_roman <sroman at umassd.edu>
To: r-help at r-project.org
Cc: 
Sent: Thursday, October 25, 2012 10:19 AM
Subject: [R] trying ti use a function in aggregate

Hi -I am using R v 2.13.0.  I am trying to use the aggregate function to
calculate the percent at length for each Trip_id and CommonName.  Here is a
small subset of the data.  
   Trip_id          Vessel       CommonName Length Count
1      230        Sunlight    Shad,American     19     1
2      230        Sunlight    Shad,American     20     1
3      230        Sunlight    Shad,American     21     1
4      230        Sunlight    Shad,American     23     1
5      230        Sunlight    Shad,American     26     1
6      230        Sunlight    Shad,American     27     1
7      230        Sunlight    Shad,American     30     2
8      230        Sunlight    Shad,American     33     1
9      230        Sunlight    Shad,American     34     1
10     230        Sunlight    Shad,American     37     1
11     230        Sunlight Herring,Blueback     20     1
12     230        Sunlight Herring,Blueback     21     2
13     230        Sunlight Herring,Blueback     22     5
14     230        Sunlight Herring,Blueback     26     1
15     230        Sunlight          Alewife     17     1
16     230        Sunlight          Alewife     18     1
17     230        Sunlight          Alewife     20     2
18     230        Sunlight          Alewife     21     4
19     230        Sunlight          Alewife     22    16
20     230        Sunlight          Alewife     23    22
21     230        Sunlight          Alewife     24    16
22     230        Sunlight          Alewife     25     4
23     230        Sunlight          Alewife     26     1
24     230        Sunlight          Alewife     27     2
25     230        Sunlight          Alewife     28     2
26     231 Western Venture    Shad,American     23     1
27     231 Western Venture    Shad,American     24     1
28     231 Western Venture    Shad,American     25     1
29     231 Western Venture    Shad,American     28     2
30     231 Western Venture    Shad,American     29     2

My code is:
myfun<-function (x) x/sum(x)
b<-with(data,aggregate(x=list(Percent=Count),by=list(Trip_id=Trip_id,Length=Length,Species=CommonName),
FUN="myfun"))

My issue is that the percent is not be calculated by Trip_id and CommonName. 
The result is that each row has a percent of 1 indicating that myfun is not
dividing by the sum of counts with a Trip_id/CommonName group.  Any help
would be appreciated.
Thank you 





--
View this message in context: http://r.789695.n4.nabble.com/trying-ti-use-a-function-in-aggregate-tp4647414.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.




More information about the R-help mailing list