Hi all, I'm trying to compute a mean on my data but I'm struggling with 2 things: 1. getting the right layout and 2. including the missing values in the outcome. #Input data: Stock <- c("A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B") Soil <- c("Blank", "Blank", "Control", "Control", "Clay", "Clay", "Blank", "Blank", "Control", "Control", "Clay", "Clay") Nitrogen <- c(NA, NA, 0, 0, 20, 20, NA, NA, 0, 0, 20, 20) Respiration <- c(112, 113, 124, 126, 139, 137, 109, 111, 122, 124, 134, 136) d <- as.data.frame(cbind(Stock, Soil, Nitrogen, Respiration)) #Outcome I'd like to get: Stockr <- c("A", "A", "A", "B", "B", "B") Soilr <- c("Blank", "Control", "Clay", "Blank", "Control", "Clay") Nitrogenr <- c(NA, 0, 20, NA, 0, 20) Respirationr <- c(111, 125, 138, 110, 123, 135) result <- as.data.frame(cbind(Stockr, Soilr, Nitrogenr, Respirationr)) Many thanks in advance for your help! Cheers, Bea
Hello, First of all avoid as.data.frame(cbind(...)). cbind() returns a matrix and since you are mixing numbers with characters, all of the matrix elements become character. Then as.data.frame transforms everything into factors. The correct way is d <- data.frame(Stock, Soil, Nitrogen, Respiration) As for your question, try the following. r1 <- aggregate(Respiration ~ Soil + Stock, data = d, mean) r2 <- aggregate(Nitrogen ~ Soil + Stock, data = d, mean) merge(r1, r2, all = TRUE) Hope this helps, Rui Barradas Em 02-02-2014 12:32, Beatriz R. Gonzalez Dominguez escreveu:> Hi all, > > I'm trying to compute a mean on my data but I'm struggling with 2 > things: 1. getting the right layout and 2. including the missing values > in the outcome. > > #Input data: > Stock <- c("A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B") > Soil <- c("Blank", "Blank", "Control", "Control", "Clay", "Clay", > "Blank", "Blank", "Control", "Control", "Clay", "Clay") > Nitrogen <- c(NA, NA, 0, 0, 20, 20, NA, NA, 0, 0, 20, 20) > Respiration <- c(112, 113, 124, 126, 139, 137, 109, 111, 122, 124, 134, > 136) > d <- as.data.frame(cbind(Stock, Soil, Nitrogen, Respiration)) > > #Outcome I'd like to get: > Stockr <- c("A", "A", "A", "B", "B", "B") > Soilr <- c("Blank", "Control", "Clay", "Blank", "Control", "Clay") > Nitrogenr <- c(NA, 0, 20, NA, 0, 20) > Respirationr <- c(111, 125, 138, 110, 123, 135) > result <- as.data.frame(cbind(Stockr, Soilr, Nitrogenr, Respirationr)) > > Many thanks in advance for your help! > > Cheers, > > Bea > > ______________________________________________ > 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.
Not exactly the order you specified but otherwise I think this works. library(plyr) d <- data.frame(Stock, Soil, Nitrogen, Respiration) ddply(d, .(Soil, Stock), summarize, mean(Nitrogen), mean(Respiration)) John Kane Kingston ON Canada> -----Original Message----- > From: aguitatierra at hotmail.com > Sent: Sun, 2 Feb 2014 13:32:57 +0100 > To: r-help at r-project.org > Subject: [R] aggregate()? > > Hi all, > > I'm trying to compute a mean on my data but I'm struggling with 2 > things: 1. getting the right layout and 2. including the missing values > in the outcome. > > #Input data: > Stock <- c("A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B") > Soil <- c("Blank", "Blank", "Control", "Control", "Clay", "Clay", > "Blank", "Blank", "Control", "Control", "Clay", "Clay") > Nitrogen <- c(NA, NA, 0, 0, 20, 20, NA, NA, 0, 0, 20, 20) > Respiration <- c(112, 113, 124, 126, 139, 137, 109, 111, 122, 124, 134, > 136) > d <- as.data.frame(cbind(Stock, Soil, Nitrogen, Respiration)) > > #Outcome I'd like to get: > Stockr <- c("A", "A", "A", "B", "B", "B") > Soilr <- c("Blank", "Control", "Clay", "Blank", "Control", "Clay") > Nitrogenr <- c(NA, 0, 20, NA, 0, 20) > Respirationr <- c(111, 125, 138, 110, 123, 135) > result <- as.data.frame(cbind(Stockr, Soilr, Nitrogenr, Respirationr)) > > Many thanks in advance for your help! > > Cheers, > > Bea > > ______________________________________________ > 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.____________________________________________________________ GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys Works with AIM?, MSN? Messenger, Yahoo!? Messenger, ICQ?, Google Talk? and most webmails