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 <akjone...@gmail.com> To: arun <smartpink...@yahoo.com> Cc: R help <r-help@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 <smartpink...@yahoo.com> wrote: > 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 <akjone...@gmail.com> > To: r-help@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@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@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.