On Mon, 2007-05-28 at 10:58 +0800, Ruixin ZHU wrote: > Hi everybody, > > When I followed a practice example, I got an error as follows: > ######################################################################## > ####################################### > > cc<-read.table('example5_2.dat',header=TRUE) > > cc > EXAM1 EXAM2 EXAM3 EXAM4 EXAM5 > 1 45 34 23 35 50 > 2 23 36 66 66 34 > 3 67 59 72 80 69 > 4 56 43 31 34 40 > 5 74 66 57 32 66 > > mean(cc) > EXAM1 EXAM2 EXAM3 EXAM4 EXAM5 > 53.0 47.6 49.8 49.4 51.8 > > attach(cc) > > mean(EXAM1,EXAM2,EXAM3,EXAM4,EXAM5) > Error in mean.default(EXAM1, EXAM2, EXAM3, EXAM4, EXAM5) : > 'trim' must be numeric of length one
Why did you think that mean would work in the way you used it? Reading ?mean shows that the default method for mean has a first argument 'x', and second argument 'trim', plus some others. So in your 2nd example, you passed EXAM1 as argument 'x' and then EXAM2 as 'trim', and the other EXAMx variables as other arguments. R was not expecting a vector as argument 'trim' and rightly complained. The reason the first example worked is that there is a method for data frames (see the first entry in the usage section of ?mean) - where you correctly passed cc as argument 'x' as the function/method requires. > In addition: Warning message: > the condition has length > 1 and only the first element will be used in: > if (na.rm) x <- x[!is.na(x)] > Would anybody explain which caused this error, and how to modify it? What is wrong with the first example you used? Why do you need to get the means by specifying all the variables explicitly? There are various ways of getting means other than mean(): lapply(cc, mean) sapply(cc, mean) colMeans(cc) If you want specific columns, either subset the returned object: mean(cc)[c("EXAM1", "EXAM4")] or subset the object before calculating the means: mean(cc[, c("EXAM1", "EXAM4")]) ^^^ note the extra "," as we need to specify the columns here. You will need to explain more clearly what you want to do if the above is not sufficient to solve your problem. Also, be wary of overly using attach. It can be a handy little tool, until it bites you in the ass because you forgot to detach/reattach the object after making some really critical change to the underlying data/object. HTH G > > Thanks! > _____________________________________________ > Dr.Ruixin ZHU > Shanghai Center for Bioinformation Technology > [EMAIL PROTECTED] > [EMAIL PROTECTED] > 86-21-13040647832 > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch 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. -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [t] +44 (0)20 7679 0522 ECRC [f] +44 (0)20 7679 0565 UCL Department of Geography Pearson Building [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street London, UK [w] http://www.ucl.ac.uk/~ucfagls/ WC1E 6BT [w] http://www.freshwaters.org.uk/ %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% ______________________________________________ R-help@stat.math.ethz.ch 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.