Skip to content
Back to formatted view

Raw Message

Message-ID: <BLU0-SMTP157B6713523718B3D943498D9A80@phx.gbl>
Date: 2014-02-02T12:32:57Z
From: Beatriz González Domínguez
Subject: 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