Skip to content
Prev 317774 / 398506 Next

cumulative sum by group and under some criteria

Hi,


"Yes, I wanted to expand directly from d. If there are other variables, 
says "A", "B", "C" that I need to keep ?in the final expanded data, how 
to modify the code?"

d<-data.frame()
for (m1 in 2:3) {
??? for (n1 in 2:2) {
??????? for (x1 in 0:(m1-1)) {
??????????? for (y1 in 0:(n1-1)) {
d<-rbind(d,c(m1,n1,x1,y1))
}
}
}}
colnames(d)<-c("m1","n1","x1","y1")
d
res1<-do.call(rbind,lapply(unique(d$m1),function(m1) 
do.call(rbind,lapply(unique(d$n1),function(n1) 
do.call(rbind,lapply(0:(m1-1),function(x1) 
do.call(rbind,lapply(0:(n1-1),function(y1) 
do.call(rbind,lapply((m1+2):(7-n1),function(m) 
do.call(rbind,lapply((n1+2):(9-m),function(n) 
do.call(rbind,lapply(x1:(x1+m-m1), function(x) 
expand.grid(m1,n1,x1,y1,m,n,x)) ))))))))))))) 
?names(res1)<- c("m1","n1","x1","y1","m","n","x") 

set.seed(235)
?d$A<- sample(1:50,10,replace=TRUE)
?set.seed(23)
?d$B<- sample(1:50,10,replace=TRUE)

?d
?#? m1 n1 x1 y1? A? B
#1?? 2? 2? 0? 0 50 29
#2?? 2? 2? 0? 1 40 12
#3?? 2? 2? 1? 0 31 17
#4?? 2? 2? 1? 1? 7 36
#5?? 3? 2? 0? 0 13 41
#6?? 3? 2? 0? 1 27 22
#7?? 3? 2? 1? 0 49 49
#8?? 3? 2? 1? 1 47 49
#9?? 3? 2? 2? 0 23 43
#10? 3? 2? 2? 1? 4 50
library(plyr)
res2<- join(res1,d,by=c("m1","n1","x1","y1"),type="full")
res2
?#? m1 n1 x1 y1 m n x? A? B
#1?? 2? 2? 0? 0 4 4 0 50 29
#2?? 2? 2? 0? 0 4 4 1 50 29
#3?? 2? 2? 0? 0 4 4 2 50 29
#4?? 2? 2? 0? 0 4 5 0 50 29
#5?? 2? 2? 0? 0 4 5 1 50 29
#6?? 2? 2? 0? 0 4 5 2 50 29
#7?? 2? 2? 0? 0 5 4 0 50 29
#8?? 2? 2? 0? 0 5 4 1 50 29
#9?? 2? 2? 0? 0 5 4 2 50 29
#10? 2? 2? 0? 0 5 4 3 50 29
#11? 2? 2? 0? 1 4 4 0 40 12
#12? 2? 2? 0? 1 4 4 1 40 12
#13? 2? 2? 0? 1 4 4 2 40 12
#14? 2? 2? 0? 1 4 5 0 40 12
#15? 2? 2? 0? 1 4 5 1 40 12
#16? 2? 2? 0? 1 4 5 2 40 12
#17? 2? 2? 0? 1 5 4 0 40 12
#18? 2? 2? 0? 1 5 4 1 40 12
#19? 2? 2? 0? 1 5 4 2 40 12
#20? 2? 2? 0? 1 5 4 3 40 12
#21? 2? 2? 1? 0 4 4 1 31 17
#22? 2? 2? 1? 0 4 4 2 31 17
#23? 2? 2? 1? 0 4 4 3 31 17
#24? 2? 2? 1? 0 4 5 1 31 17
#25? 2? 2? 1? 0 4 5 2 31 17
#26? 2? 2? 1? 0 4 5 3 31 17
#27? 2? 2? 1? 0 5 4 1 31 17
#28? 2? 2? 1? 0 5 4 2 31 17
#29? 2? 2? 1? 0 5 4 3 31 17
#30? 2? 2? 1? 0 5 4 4 31 17
#31? 2? 2? 1? 1 4 4 1? 7 36
#32? 2? 2? 1? 1 4 4 2? 7 36
#33? 2? 2? 1? 1 4 4 3? 7 36
#34? 2? 2? 1? 1 4 5 1? 7 36
#35? 2? 2? 1? 1 4 5 2? 7 36
#36? 2? 2? 1? 1 4 5 3? 7 36
#37? 2? 2? 1? 1 5 4 1? 7 36
#38? 2? 2? 1? 1 5 4 2? 7 36
#39? 2? 2? 1? 1 5 4 3? 7 36
#40? 2? 2? 1? 1 5 4 4? 7 36
#41? 3? 2? 0? 0 5 4 0 13 41
#42? 3? 2? 0? 0 5 4 1 13 41
#43? 3? 2? 0? 0 5 4 2 13 41
#44? 3? 2? 0? 1 5 4 0 27 22
#45? 3? 2? 0? 1 5 4 1 27 22
#46? 3? 2? 0? 1 5 4 2 27 22
#47? 3? 2? 1? 0 5 4 1 49 49
#48? 3? 2? 1? 0 5 4 2 49 49
#49? 3? 2? 1? 0 5 4 3 49 49
#50? 3? 2? 1? 1 5 4 1 47 49
#51? 3? 2? 1? 1 5 4 2 47 49
#52? 3? 2? 1? 1 5 4 3 47 49
#53? 3? 2? 2? 0 5 4 2 23 43
#54? 3? 2? 2? 0 5 4 3 23 43
#55? 3? 2? 2? 0 5 4 4 23 43
#56? 3? 2? 2? 1 5 4 2? 4 50
#57? 3? 2? 2? 1 5 4 3? 4 50
#58? 3? 2? 2? 1 5 4 4? 4 50
A.K.








----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: Joanna Zhang <zjoanna2013 at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Saturday, February 16, 2013 11:49 PM
Subject: Re: [R] cumulative sum by group and under some criteria



d2<- data.frame()
for (m1 in 2:3) {
??? for (n1 in 2:2) {
??????? for (x1 in 0:(m1-1)) {
??????????? for (y1 in 0:(n1-1)) {
??? ??? for (m in (m1+2): (7-n1)){
???? ??? ??? ? for (n in (n1+2):(9-m)){
? ??? ??? ???? for (x in x1:(x1+m-m1)){ 
?d2<- rbind(d2,c(m1,n1,x1,y1,m,n,x))
?}}}}}}}
colnames(d2)<-c("m1","n1","x1","y1","m","n","x")

res<-do.call(rbind,lapply(2:3,function(m1) do.call(rbind,lapply(2:2,function(n1) do.call(rbind,lapply(0:(m1-1),function(x1) do.call(rbind,lapply(0:(n1-1),function(y1) do.call(rbind,lapply((m1+2):(7-n1),function(m) do.call(rbind,lapply((n1+2):(9-m),function(n) do.call(rbind,lapply(x1:(x1+m-m1), function(x) expand.grid(m1,n1,x1,y1,m,n,x)) )))))))))))))
names(res)<- c("m1","n1","x1","y1","m","n","x")
?attr(res,"out.attrs")<-NULL
?identical(d2,res)
#[1] TRUE
A.K.