[R] Mean calculation by two variables

arun smartpink111 at yahoo.com
Thu Jan 17 06:53:05 CET 2013


HI,

May be this helps:
Example$Wi<-unlist(aggregate(Weight~ID,data=Example,function(x) round(x/sum(x),2))[,2])
res<-do.call(rbind,lapply(split(Example,Example$Specie),function(x) with(x, {aggregate(Wi,list(Food.item),function(y) sum(y)/length(unique(x[,1])))})))

names(res)<-names(Solution)[2:3]
 res1<-data.frame(Specie=gsub("[.0-9]","",row.names(res)),res)
row.names(res1)<-1:nrow(res1)

 res1
 #                    Specie Food.item       MWi
#1 Acestrorhynchus lacustris      Alga 0.0075000
#2 Acestrorhynchus lacustris      Fish 0.5000000
#3 Acestrorhynchus lacustris       MNI 0.2350000
#4 Acestrorhynchus lacustris  Sediment 0.0075000
#5 Acestrorhynchus lacustris   Vegetal 0.2500000
#6     Schizodon intermedius      Alga 0.1411111
#7     Schizodon intermedius       MNI 0.1844444
#8     Schizodon intermedius  Sediment 0.1411111
#9     Schizodon intermedius   Vegetal 0.5322222
 Solution
#                     Specie Food.item         MWi
#1 Acestrorhynchus lacustris     Peixe         0.5
#2 Acestrorhynchus lacustris   Vegetal        0.25
#3 Acestrorhynchus lacustris      Alga      0.0075
# Acestrorhynchus lacustris       MNI       0.235
#5 Acestrorhynchus lacustris Sedimento      0.0075
#6     Schizodon intermedius      Alga 0.141111111
#7     Schizodon intermedius       MNI 0.184444444
#8     Schizodon intermedius   Vegetal 0.532222222
#9     Schizodon intermedius Sedimento 0.141111111


A.K.

----- Original Message -----
From: Raoni Rodrigues <caciquesamurai at gmail.com>
To: Rui Barradas <ruipbarradas at sapo.pt>
Cc: r-help at r-project.org
Sent: Wednesday, January 16, 2013 11:23 PM
Subject: Re: [R] Mean calculation by two variables

Hello Rui!

Thanks a lot for your help!

Sorry for make mistake with the factor's name, was inattention during data
preparation for the asking mail.

Unfortunately, it not simple mean calculation, because same individual
could have more than one row, if it eat more than a food item. In example
data, Acestrorhynchus lacustris are represent by six rows and just four
individuals (ID 1717 repeats three times). Because that I tried use "lenght
(unique(ID))".

After calculate the proportion for each individual (step1), I have to sum
same food item and divide by four for Acestrorhynchus and by nine for
Schizodon (that have 14 row of data). Because that is not simply calculate
the mean.

Could you please give me some idea?

I'm sending again exemple and solution data, now with correct factor's name.

Thanks again for your attention and time,

Raoni

*Example:*
structure(list(ID = c(779L, 782L, 1717L, 1717L, 1717L, 1803L,
2650L, 2650L, 2700L, 2700L, 3611L, 3613L, 3647L, 3654L, 3654L,
3683L, 3683L, 3683L, 3685L, 3997L), Site = c("Três Marias", "Três Marias",
"Nova Ponte", "Nova Ponte", "Nova Ponte", "Três Marias", "São Simão",
"São Simão", "São Simão", "São Simão", "São Simão", "São Simão",
"São Simão", "São Simão", "São Simão", "São Simão", "São Simão",
"São Simão", "São Simão", "São Simão"), Specie = c("Acestrorhynchus
lacustris",
"Acestrorhynchus lacustris", "Acestrorhynchus lacustris", "Acestrorhynchus
lacustris",
"Acestrorhynchus lacustris", "Acestrorhynchus lacustris", "Schizodon
intermedius",
"Schizodon intermedius", "Schizodon intermedius", "Schizodon intermedius",
"Schizodon intermedius", "Schizodon intermedius", "Schizodon intermedius",
"Schizodon intermedius", "Schizodon intermedius", "Schizodon intermedius",
"Schizodon intermedius", "Schizodon intermedius", "Schizodon intermedius",
"Schizodon intermedius"), Weight = c(0.06, 0.01, 0.01, 0.33,
0.01, 3.5, 0.01, 0.04, 0.01, 0.01, 0.38, 0.29, 0.04, 0.03, 0.11,
0.04, 0.04, 0.03, 0.01, 0.01), Food.item = c("Fish", "Vegetal",
"Alga", "MNI", "Sediment", "Fish", "Alga", "MNI", "Alga", "MNI",
"Vegetal", "Vegetal", "Vegetal", "Alga", "Vegetal", "Alga", "MNI",
"Sediment", "Sediment", "Vegetal")), .Names = c("ID", "Site",
"Specie", "Weight", "Food.item"), row.names = c(1869113L, 2290407L,
56668L, 1485394L, 2126489L, 368143L, 57601L, 1486327L, 57651L,
1486377L, 2348187L, 2348189L, 2293272L, 58605L, 2293279L, 40317L,
1487360L, 2128455L, 2128457L, 2293622L), class = "data.frame")

*Solution:*
structure(list(Specie = c("Acestrorhynchus lacustris", "Acestrorhynchus
lacustris",
"Acestrorhynchus lacustris", "Acestrorhynchus lacustris", "Acestrorhynchus
lacustris",
"Schizodon intermedius", "Schizodon intermedius", "Schizodon intermedius",
"Schizodon intermedius"), Food.item = c("Peixe", "Vegetal", "Alga",
"MNI", "Sedimento", "Alga", "MNI", "Vegetal", "Sedimento"), MWi = c("0.5",
"0.25", "0.0075", "0.235", "0.0075", "0.141111111", "0.184444444",
"0.532222222", "0.141111111")), .Names = c("Specie", "Food.item",
"MWi"), row.names = c(NA, -9L), class = "data.frame")



2013/1/16 Rui Barradas <ruipbarradas at sapo.pt>

>  Hello,
>
> If you want to calculate the mean weight by Specie and Food.item, you can
> use ?aggregate.
> In what follows, I've named your EXAMPLE ex and your SOLUTION sol. Note
> that the result is different from sol.
> (No exemplo não há Peixe, é Fish, e os números são outros.)
>
> aggregate(Weight ~ Specie + Food.item, data = ex, FUN = mean)
>
>
> Hope this helps,
>
> Rui Barradas
> Em 16-01-2013 15:41, Raoni Rodrigues escreveu:
>
> Hello All,
>
> I have a data frame (dput information below) with food item weight for fish
> species.
>
> I need to calculate the Mean proportion by weight of each food item for
> each specie, as show in solution data frame (dput information below).
>
> I use the ddply function (plyr package) in two steps. First calculate the
> proportion of weight for each individual:
>
> step1 = ddply (example, .(ID), transform, Wi = round (Weight/sum (Weight),
> 2))
>
> Then, I use lenght (unique (ID)) to calculate the mean of each food item
> for each specie:
>
> step2 = ddply (step1, .(Specie, Food.item), summarise, MWi = sum
> (Wi)/length (unique(ID)))
>
> I do not understand why this didn't work. Someone can help me?
>
> Thanks in advanced!
>
>
> *EXAMPLE*
> structure(list(ID = c(779L, 782L, 1717L, 1717L, 1717L, 1803L,
> 2650L, 2650L, 2700L, 2700L, 3611L, 3613L, 3647L, 3654L, 3654L,
> 3683L, 3683L, 3683L, 3685L, 3997L), Site = c("Três Marias", "Três Marias",
> "Nova Ponte", "Nova Ponte", "Nova Ponte", "Três Marias", "São Simão",
> "São Simão", "São Simão", "São Simão", "São Simão", "São Simão",
> "São Simão", "São Simão", "São Simão", "São Simão", "São Simão",
> "São Simão", "São Simão", "São Simão"), Specie = c("Acestrorhynchus
> lacustris",
> "Acestrorhynchus lacustris", "Acestrorhynchus lacustris", "Acestrorhynchus
> lacustris",
> "Acestrorhynchus lacustris", "Acestrorhynchus lacustris", "Schizodon
> intermedius",
> "Schizodon intermedius", "Schizodon intermedius", "Schizodon intermedius",
> "Schizodon intermedius", "Schizodon intermedius", "Schizodon intermedius",
> "Schizodon intermedius", "Schizodon intermedius", "Schizodon intermedius",
> "Schizodon intermedius", "Schizodon intermedius", "Schizodon intermedius",
> "Schizodon intermedius"), Weight = c(0.06, 0.01, 0.01, 0.33, 0.01,
> 3.5, 0.01, 0.04, 0.01, 0.01, 0.38, 0.29, 0.04, 0.03, 0.11, 0.04,
> 0.04, 0.03, 0.01, 0.01), Food.item = c("Fish", "Vegetal", "Alga",
> "MNI", "Sediment", "Fish", "Alga", "MNI", "Alga", "MNI", "Vegetal",
> "Vegetal", "Vegetal", "Alga", "Vegetal", "Alga", "MNI", "Sediment",
> "Sediment", "Vegetal")), .Names = c("ID", "Site", "Specie",
> "Weight", "Food.item"), row.names = c(1869113L, 2290407L, 56668L,
> 1485394L, 2126489L, 368143L, 57601L, 1486327L, 57651L, 1486377L,
> 2348187L, 2348189L, 2293272L, 58605L, 2293279L, 40317L, 1487360L,
> 2128455L, 2128457L, 2293622L), class = "data.frame")
>
>
> *SOLUTION*
> structure(list(Specie = c("Acestrorhynchus lacustris", "Acestrorhynchus
> lacustris",
> "Acestrorhynchus lacustris", "Acestrorhynchus lacustris", "Acestrorhynchus
> lacustris",
> "Schizodon intermedius", "Schizodon intermedius", "Schizodon intermedius",
> "Schizodon intermedius"), Food.item = c("Peixe", "Vegetal", "Alga",
> "MNI", "Sedimento", "Alga", "MNI", "Vegetal", "Sedimento"), MWi = c("0.5",
> "0.25", "0.0075", "0.235", "0.0075", "0.141111111", "0.184444444",
> "0.532222222", "0.141111111")), .Names = c("Specie", "Food.item",
> "MWi"), class = "data.frame", row.names = c(NA, -9L))
> *
> *
> sessionInfo()
> R version 2.15.2 (2012-10-26)
> Platform: i386-w64-mingw32/i386 (32-bit)
> Windows XP
> *
> *
>
>
>
> ______________________________________________R-help at r-project.org mailing listhttps://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.
>
>
>


-- 
Raoni Rosa Rodrigues
Research Associate of Fish Transposition Center CTPeixes
Universidade Federal de Minas Gerais - UFMG
Brasil
rodrigues.raoni at gmail.com

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




More information about the R-help mailing list