סכומי ביניים

10 views
Skip to first unread message

ronen

unread,
Aug 27, 2014, 3:11:35 AM8/27/14
to israel-r-...@googlegroups.com
I have a big data base
I want to do the following partial sums:
 
X=matrix(1:64,8,8)
y=rep(1:2,c(4,4))
psum=by(X,y,apply,2,sum)
print(psum)
 
Is there a better/quicker way to do it?
Thank you
Ronen
 

Jonathan Rosenblatt

unread,
Aug 27, 2014, 3:44:42 AM8/27/14
to israel-r-user-group
What exactly is the problem?
Long runtime? Crashing? Opaque code?


Ronen
 

--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Jonathan Rosenblatt
www.john-ros.com

ronen

unread,
Aug 27, 2014, 4:20:36 AM8/27/14
to israel-r-...@googlegroups.com
no problem just wondered if this is how it should be done
to optimize my work
Thank you!
 

On Wednesday, August 27, 2014 10:44:42 AM UTC+3, Jonathan Rosenblatt wrote:
What exactly is the problem?
Long runtime? Crashing? Opaque code?
On Wed, Aug 27, 2014 at 10:11 AM, ronen <rfl...@gmail.com> wrote:
I have a big data base
I want to do the following partial sums:
 
X=matrix(1:64,8,8)
y=rep(1:2,c(4,4))
psum=by(X,y,apply,2,sum)
print(psum)
 
Is there a better/quicker way to do it?
Thank you
Ronen
 

--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-group+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Jonathan Rosenblatt
www.john-ros.com

Jonathan Rosenblatt

unread,
Aug 27, 2014, 6:42:15 AM8/27/14
to israel-r-user-group
(a) Readability could be improved. Recall that future-self will have to understand the code. I would loop over categories of y as you will lose nothing in efficiency (by() simply wraps around tapply() which is nothing but a loop), but will gain on readability. 

(b) I think that rowSums and colSums are slightly more efficiency that apply(X,2,sum).

(c) The dplyr package is super efficient for data manipulations. You might get good performance by compounding dplyr::group_by and dplyr::summarise. It is actually a package worth knowing anyhow....





To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Jonathan Rosenblatt
www.john-ros.com

ronen

unread,
Aug 27, 2014, 6:56:42 AM8/27/14
to israel-r-...@googlegroups.com
Thank you !
I will look into this package.
Ronen
 

On Wednesday, August 27, 2014 1:42:15 PM UTC+3, Jonathan Rosenblatt wrote:
(a) Readability could be improved. Recall that future-self will have to understand the code. I would loop over categories of y as you will lose nothing in efficiency (by() simply wraps around tapply() which is nothing but a loop), but will gain on readability. 

(b) I think that rowSums and colSums are slightly more efficiency that apply(X,2,sum).

(c) The dplyr package is super efficient for data manipulations. You might get good performance by compounding dplyr::group_by and dplyr::summarise. It is actually a package worth knowing anyhow....



On Wed, Aug 27, 2014 at 11:20 AM, ronen <rfl...@gmail.com> wrote:
no problem just wondered if this is how it should be done
to optimize my work
Thank you!
 

On Wednesday, August 27, 2014 10:44:42 AM UTC+3, Jonathan Rosenblatt wrote:
What exactly is the problem?
Long runtime? Crashing? Opaque code?
On Wed, Aug 27, 2014 at 10:11 AM, ronen <rfl...@gmail.com> wrote:
I have a big data base
I want to do the following partial sums:
 
X=matrix(1:64,8,8)
y=rep(1:2,c(4,4))
psum=by(X,y,apply,2,sum)
print(psum)
 
Is there a better/quicker way to do it?
Thank you
Ronen
 

--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-group+unsubscribe...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Jonathan Rosenblatt
www.john-ros.com

--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-group+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Jonathan Rosenblatt
www.john-ros.com

Reply all
Reply to author
Forward
0 new messages