Skip to content

cumulative sum by group and under some criteria

4 messages · Zjoanna, arun

#
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.
#
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.
1 day later
#
Hi,

If you don't want the m1=3, n1=3 combination in the final dataset:
library(plyr)
res2<- join(res1,d3,by=c("m1","n1"),type="inner")
tail(res2)
#??? m1 n1 x1 y1 m n x y cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H
#235? 3? 2? 2? 1 5 4 3 1?? 0.857375????? 0.512???? 0.9025?????? 0.64
#236? 3? 2? 2? 1 5 4 3 2?? 0.857375????? 0.512???? 0.9025?????? 0.64
#237? 3? 2? 2? 1 5 4 3 3?? 0.857375????? 0.512???? 0.9025?????? 0.64
#238? 3? 2? 2? 1 5 4 4 1?? 0.857375????? 0.512???? 0.9025?????? 0.64
#239? 3? 2? 2? 1 5 4 4 2?? 0.857375????? 0.512???? 0.9025?????? 0.64
#240? 3? 2? 2? 1 5 4 4 3?? 0.857375????? 0.512???? 0.9025?????? 0.64
A.K.




----- Original Message -----
From: Zjoanna <Zjoanna2013 at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Monday, February 18, 2013 10:36 PM
Subject: Re: [R] cumulative sum by group and under some criteria

Thanks. I tried this code below, why does expanded dataset 'res1' has m1=3
and n1=3
, dataset 'd3' doesn't have m1=3, n1=3.


d3<-structure(list(m1 = c(2, 3, 2), n1 = c(2, 2, 3), cterm1_P0L = c(0.9025,
0.857375, 0.9025), cterm1_P1L = c(0.64, 0.512, 0.64), cterm1_P0H =
c(0.9025,
0.9025, 0.857375), cterm1_P1H = c(0.64, 0.64, 0.512)), .Names = c("m1",
"n1", "cterm1_P0L", "cterm1_P1L", "cterm1_P0H", "cterm1_P1H"), row.names =
c(NA,
3L), class = "data.frame")
d3

res1<-do.call(rbind,lapply(unique(d3$m1),function(m1)
do.call(rbind,lapply(unique(d3$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)
do.call(rbind,lapply(y1:(y1+n-n1), function(y)
expand.grid(m1,n1,x1,y1,m,n,x,y)) )))))))))))))))
names(res1)<- c("m1","n1","x1","y1","m","n","x","y")
attr(res1,"out.attrs")<-NULL
res1

library(plyr)
res2<- join(res1,d3,by=c("m1","n1"),type="full")
res2

On Sun, Feb 17, 2013 at 11:10 PM, arun kirshna [via R] <
ml-node+s789695n4658895h56 at n4.nabble.com> wrote:

            
--
View this message in context: http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4659018.html
Sent from the R help mailing list archive at Nabble.com.
??? [[alternative HTML version deleted]]

______________________________________________
R-help at 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.