Skip to content
Prev 311319 / 398506 Next

Coefficient of Variation, NA, Aggregate

HI,

Your example dataset is in unreadable format.? You could use dput().
set.seed(5)
?mat1<-matrix(sample(c(1:9,NA),20,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],2),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?????? NA 70.710678?????? NA 20.20305 28.28427
2?????? B 56.56854? 8.318903 60.60915 47.14045? 0.00000


With your aggregate()
aggregate(.~subspecies,data=rowleyi,co.var)
#? subspecies?????? X1?????? X2?????? X3?????? X4 X5
#1????????? B 56.56854 8.318903 60.60915 47.14045? 0

A.K.




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

Hello helpers,

I have a two part issue. FIRSTLY, I am attempting to write a function
for coefficient of variation, using
This is not working because some of my columns have NAs. When I try to use
Error in co.var(rowleyi$TL, na.rm = TRUE) :
? unused argument(s) (na.rm = TRUE)

I do not know what this means. How can I get this function to work?
SECONDLY, how can I then get that function to work within an
aggragate? Do I still use
Thank you!
Amanda Jones

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