Using apply

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Using apply

Steven Yen
I need help with "apply". Below, I have no problem getting the column sums.
1. How do I get the sum of squares?
2. In general, where do I look up these functions?
Thanks.

x<-matrix(1:10,nrow=5); x
sum <- apply(x,2,sum); sum




        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Using apply

jholtman
> s2 <- apply(x*x, 2, sum)
> s2
[1]  55 330

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Tue, Oct 30, 2018 at 10:28 PM Steven Yen <[hidden email]> wrote:

>
> I need help with "apply". Below, I have no problem getting the column sums.
> 1. How do I get the sum of squares?
> 2. In general, where do I look up these functions?
> Thanks.
>
> x<-matrix(1:10,nrow=5); x
> sum <- apply(x,2,sum); sum
>
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Using apply

plangfelder
In reply to this post by Steven Yen
It should be said that for many basic statistics, there are faster
functions than apply, for example here you want

sum = colSums(x)

As already said, for sum of squares you would do colSums(x^2).

Many useful functions of this kind are implemented in package
matrixStats. Once you install it, either look at the package manual or
type ls("package:matrixStats") to see a list of functions. Most if not
all have self-explanatory names.

HTH,

Peter
On Tue, Oct 30, 2018 at 7:28 PM Steven Yen <[hidden email]> wrote:

>
> I need help with "apply". Below, I have no problem getting the column sums.
> 1. How do I get the sum of squares?
> 2. In general, where do I look up these functions?
> Thanks.
>
> x<-matrix(1:10,nrow=5); x
> sum <- apply(x,2,sum); sum
>
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Using apply

Bert Gunter-2
Indeed.

But perhaps it's also worth noting that if such statistics are calculated
as implementations of (e.g. anova) formulae still found (sadly) in many
statistics texts, then they shouldn't be calculated at all. Rather, the
appropriate matrix methods (e.g. QR decompositions ) built into R -- many
of which are already incorporated into R's statistical corpus -- should be
used. To say more would of course be far O/T.

Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Tue, Oct 30, 2018 at 8:44 PM Peter Langfelder <[hidden email]>
wrote:

> It should be said that for many basic statistics, there are faster
> functions than apply, for example here you want
>
> sum = colSums(x)
>
> As already said, for sum of squares you would do colSums(x^2).
>
> Many useful functions of this kind are implemented in package
> matrixStats. Once you install it, either look at the package manual or
> type ls("package:matrixStats") to see a list of functions. Most if not
> all have self-explanatory names.
>
> HTH,
>
> Peter
> On Tue, Oct 30, 2018 at 7:28 PM Steven Yen <[hidden email]> wrote:
> >
> > I need help with "apply". Below, I have no problem getting the column
> sums.
> > 1. How do I get the sum of squares?
> > 2. In general, where do I look up these functions?
> > Thanks.
> >
> > x<-matrix(1:10,nrow=5); x
> > sum <- apply(x,2,sum); sum
> >
> >
> >
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: Using apply

Rolf Turner
In reply to this post by jholtman

On 10/31/18 3:47 PM, jim holtman wrote:

>> s2 <- apply(x*x, 2, sum)
>> s2
> [1]  55 330

<SNIP>

It seems to me to be more "natural" (and perhaps more amenable to
generalisation) to do:

     s2 <- apply(x,2,function(v){sum(v^2)})

But it's probably just a matter of taste.

cheers,

Rolf

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: Using apply

Jeff Newmiller
In general doing vectorized calculations on larger data units is going to lead to faster computation, so it would be better in the long run if your taste could evolve to appreciate Jim's approach.

On October 30, 2018 10:03:15 PM PDT, Rolf Turner <[hidden email]> wrote:

>
>On 10/31/18 3:47 PM, jim holtman wrote:
>
>>> s2 <- apply(x*x, 2, sum)
>>> s2
>> [1]  55 330
>
><SNIP>
>
>It seems to me to be more "natural" (and perhaps more amenable to
>generalisation) to do:
>
>     s2 <- apply(x,2,function(v){sum(v^2)})
>
>But it's probably just a matter of taste.
>
>cheers,
>
>Rolf

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.