[R] Coefficient of Variation, NA, Aggregate

arun smartpink111 at yahoo.com
Mon Nov 19 23:29:33 CET 2012


HI,
For the first part, may be this helps:
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))
 apply(rowleyi,2,function(x) co.var(x))
#      X1       X2       X3       X4       X5 
#53.29387 49.53113 45.82576 35.35534 34.99271 
#or
sapply(rowleyi,function(x) co.var(x))
#      X1       X2       X3       X4       X5 
#53.29387 49.53113 45.82576 35.35534 34.99271 
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

> co.var <- function(rowleyi) ( 100*sd(rowleyi)/mean(rowleyi) )  #where rowleyi is my data set, which has multiple columns and rows of data.

This is not working because some of my columns have NAs. When I try to use

> co.var(rowleyi$TL, na.rm=TRUE)  #where TL is one of my column names, it gives me an error message:

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

>aggregate(. ~ subspecies, data = rowleyi, CV, na.rm=TRUE) #where subspecies is the header for rows? This has worked for mean, std.error, sd, etc.

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.





More information about the R-help mailing list