Yes, mean of a one element object is meanless, but I'm trying to avoid a
ifelse in the sentence.
The result of mean values of x and y is added at each loop to a data.frame
(or any other object that could speed up the loop.



2009/8/3 ONKELINX, Thierry <Thierry.ONKELINX@inbo.be>

> Dear Paulo,
>
> Your object is a named vector. Apply() and colMeans() only work on
> matrices and dataframes. So you need to convert the vector to a matrix
> or data.frame. See the example below. Please not that calculating the
> mean of one element is a waiste of time.
>
> # a named numerical vector
> object <- c(x = 670, y = 314)
> str(object)
> class(object)
>
> #a data frame with two numerical vectors.
> object2 <- data.frame(x = 670, y = 314)
> str(object2)
> class(object2)
> colMeans(object2)
>
> HTH,
>
> Thierry
>
>
> ------------------------------------------------------------------------
> ----
> ir. Thierry Onkelinx
> Instituut voor natuur- en bosonderzoek / Research Institute for Nature
> and Forest
> Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
> methodology and quality assurance
> Gaverstraat 4
> 9500 Geraardsbergen
> Belgium
> tel. + 32 54/436 185
> Thierry.Onkelinx@inbo.be
> www.inbo.be
>
> To call in the statistician after the experiment is done may be no more
> than asking him to perform a post-mortem examination: he may be able to
> say what the experiment died of.
> ~ Sir Ronald Aylmer Fisher
>
> The plural of anecdote is not data.
> ~ Roger Brinner
>
> The combination of some data and an aching desire for an answer does not
> ensure that a reasonable answer can be extracted from a given body of
> data.
> ~ John Tukey
>
> -----Oorspronkelijk bericht-----
> Van: r-help-bounces@r-project.org [mailto:r-help-bounces@r-project.org]
> Namens Paulo Eduardo Cardoso
> Verzonden: maandag 3 augustus 2009 16:19
> Aan: David Winsemius
> CC: [R]
> Onderwerp: Re: [R] apply function to named numeric object
>
> Both objects are numeric, but they differ in structure and I don't know
> how to handle both with a single function to get means.
> When I try to apply colMeans to a named numeric object like this:
>
> Named num [1:2] 670 314
>  - attr(*, "names")= chr [1:2] "x" "y"
>
> I get the error messages:
>
> Error in colMeans(centroid) :
>  'x' must be an array of at least two dimensions
>
> and apply(object,2,mean)
>
> Error in apply(object, 2, mean) : dim(X) must have a positive length
>
> There are something very basic that I'm missing.
>
> 2009/8/3 David Winsemius <dwinsemius@comcast.net>
>
> >
> > On Aug 3, 2009, at 9:52 AM, Paulo Eduardo Cardoso wrote:
> >
> > Thanks. Works
> > A doubt.
> >
> > I asked that because I have a loop that can generate objects like that
>
> > or this one
> >
> >
> > str(object)
> > Named num [1:2] 670 314
> >  - attr(*, "names")= chr [1:2] "x" "y"
> >
> > To avoid using a ifelse() to calculate means of x and y, I'd like a
> > function that handle with this named numeric object and that list. Any
>
> > chance of do it with a single function?
> >
> >
> > I do not see any list object. Lists and arrays (of which the two
> > dimensional ones are of type "matrix") are quite different basic data
> > structures in R. If you interchange the use of those terms you will
> > not get useful replies from R users. Those two object both appear to
> > be matrices and both should be handled by those functions.
> >
> >
> > 2009/8/3 David Winsemius <dwinsemius@comcast.net>
> >
> >>
> >> On Aug 3, 2009, at 9:07 AM, Paulo Eduardo Cardoso wrote:
> >>
> >>  Maybe a very basic question.
> >>> How can I get mean of a object like this
> >>>
> >>>  str(object)
> >>>>
> >>> num [1:53, 1:2] 734 734 736 734 736 ...
> >>> - attr(*, "dimnames")=List of 2
> >>>  ..$ : NULL
> >>>  ..$ : chr [1:2] "x" "y"
> >>>
> >>> I want the mean of both x and y
> >>>
> >>
> >> apply(object, 2, mean)
> >>
> >> or...
> >>
> >> colMeans(object)  # which will be faster
> >>
> >>
> >>  _______________
> >>> Paulo E. Cardoso
> >>>
> >>>        [[alternative HTML version deleted]]
> >>>
> >>> __________________________________________
> >>>
> >>
> >
> > David Winsemius, MD
> > Heritage Laboratories
> > West Hartford, CT
> >
> >
>
>
> --
> _______________
> Paulo E. Cardoso
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
> Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver
> weer
> en binden het INBO onder geen enkel beding, zolang dit bericht niet
> bevestigd is
> door een geldig ondertekend document. The views expressed in  this message
> and any annex are purely those of the writer and may not be regarded as
> stating
> an official position of INBO, as long as the message is not confirmed by a
> duly
> signed document.
>



-- 
_______________
Paulo E. Cardoso

	[[alternative HTML version deleted]]


