Skip to content
Prev 311321 / 398506 Next

Coefficient of Variation, NA, Aggregate

HI,

No problem.

You got two NA in the previous example.
According to the coefficient of variaion documentation in R (http://hosho.ees.hokudai.ac.jp/~kubo/Rdoc/library/raster/html/cv.html)
Compute the coefficient of variation (expressed as a percentage). If there is only a single value, sd is NA and cv returns NA if aszero=FALSE (the default). However, if (aszero=TRUE), cv returns 0."
?
If I use another example:
set.seed(5)
?mat1<-matrix(sample(c(1:10,NA),30,replace=TRUE),ncol=5)
rowleyi<-data.frame(mat1)
co.var<-function(x) 100*(sd(x,na.rm=TRUE)/mean(x,na.rm=TRUE))
?rowleyi<-data.frame(subspecies=rep(LETTERS[1:2],3),rowleyi)

with(rowleyi,aggregate(cbind(X1,X2,X3,X4,X5),by=list(subspecies),function(x) co.var(x)))
#? Group.1?????? X1?????? X2?????? X3?????? X4?????? X5
#1?????? A 28.28427 28.28427 25.00000 52.67827 57.73503
#2?????? B 34.64102 61.97443 52.67827 51.50788?????? NA


#Other way: 
?do.call(cbind,lapply(lapply(lapply(rowleyi[,-1],function(x) data.frame(subspecies=rowleyi[,1],x)),function(x) x[complete.cases(x),]),function(x) aggregate(.~subspecies,data=x,co.var)))
#? X1.subspecies???? X1.x X2.subspecies???? X2.x X3.subspecies???? X3.x
#1???????????? A 28.28427???????????? A 28.28427???????????? A 25.00000
#2???????????? B 34.64102???????????? B 61.97443???????????? B 52.67827
? X4.subspecies???? X4.x X5.subspecies???? X5.x
#1???????????? A 52.67827???????????? A 57.73503
#2???????????? B 51.50788???????????? B?????? NA
A.K.




----- Original Message -----
From: Amanda Jones <akjones82 at gmail.com>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Monday, November 19, 2012 5:50 PM
Subject: Re: [R] Coefficient of Variation, NA, Aggregate

Fantastic, thank you!
On Mon, Nov 19, 2012 at 3:44 PM, arun <smartpink111 at yahoo.com> wrote: