[R] aggregate()?

arun smartpink111 at yahoo.com
Sun Feb 2 21:00:19 CET 2014


Probably, you are referring to:
d <- as.data.frame(cbind(Stock, Soil, Nitrogen, Respiration))###
with(d,aggregate(cbind(Nitrogenr=Nitrogen,Respirationr=Respiration),by=list(Soilr=Soil,Stockr=Stock),FUN=mean))


I used:
d <- data.frame(Stock, Soil, Nitrogen, Respiration)
with(d,aggregate(cbind(Nitrogenr=Nitrogen,Respirationr=Respiration),by=list(Soilr=Soil,Stockr=Stock),FUN=mean))


A.K.


On Sunday, February 2, 2014 2:56 PM, arun <smartpink111 at yahoo.com> wrote:
 
I understand that 'result' is different from my output. It could be a typo or so, which I don't know.  Also, I am not not sure about whether it is about the order or something else you are referring to.
merge(r1, r2, all = TRUE)#Rui's output

     Soil Stock Respiration Nitrogen
1   Blank     A       112.5       NA
2   Blank     B       110.0       NA
3    Clay     A       138.0       20
4    Clay     B       135.0       20
5 Control     A       125.0        0
6 Control     B       123.0        0


with(d,aggregate(cbind(Nitrogenr=Nitrogen,Respirationr=Respiration),by=list(Soilr=Soil,Stockr=Stock),FUN=mean))
    Soilr Stockr Nitrogenr Respirationr
1   Blank      A        NA        112.5
2    Clay      A        20        138.0
3 Control      A         0        125.0
4   Blank      B        NA        110.0
5    Clay      B        20        135.0
6 Control      B         0        123.0


OP's result
 result
  Stockr   Soilr Nitrogenr Respirationr
1      A   Blank        NA          111
2      A Control         0          125
3      A    Clay        20          138
4      B   Blank        NA          110
5      B Control         0          123
6      B    Clay        20          135
A.K.








On Sunday, February 2, 2014 1:52 PM, David Winsemius <dwinsemius at comcast.net> wrote:

On Feb 2, 2014, at 8:43 AM, arun wrote:

> Also,
> 
>  with(d,aggregate(cbind(Nitrogenr=Nitrogen,Respirationr=Respiration),by=list(Soilr=Soil,Stockr=Stock),FUN=mean))
> 

Did you compare your output to the input?

-- 
David.
> A.K.
> 
> On Sunday, February 2, 2014 7:58 AM, Rui Barradas <ruipbarradas at sapo.pt> wrote:
> 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.

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

David Winsemius
Alameda, CA, USA





More information about the R-help mailing list