Skip to content

Mean calculation by two variables

4 messages · Raoni Rodrigues, Rui Barradas, arun

#
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>