[R] Coefficient of Variation, NA, Aggregate
arun
smartpink111 at yahoo.com
Mon Nov 19 23:44:50 CET 2012
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
> 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