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 * * -- Raoni Rosa Rodrigues Research Associate of Fish Transposition Center CTPeixes Universidade Federal de Minas Gerais - UFMG Brasil rodrigues.ra...@gmail.com [[alternative HTML version deleted]]
______________________________________________ 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.