2017-08-29 58 views
0

我有雙循環(它將在最後的三重循環,但當時是一件事)。我需要將其結果保存到數據框中。我沒有爲單循環做這件事,但我有問題,當我需要做嵌套循環。我設法寫複製代碼:使用嵌套(雙)循環的結果存儲

#first i create sample df with 3 random variables and index 
df=data.frame("var1"=runif(18,min=0,max=1),` 
       "var2"=runif(18,min=0,max=1), 
       "var3"=runif(18,min=0,max=1), 
       "index2"=c(rep(c("A","B","C"),6)), 
       "index1"=c(rep(1,9),rep(2,9))) 



#lists for subseting data in loops 
list.1=list(1,2) 
list.2=list("A","B","C") 
#first loop based on list.2 
for (i in 1:length(list.2)){ 
    i2=list.2[i]#indicator for inside loop to subset based on letter 
    for (i in 1:length(list.1)){ 
     x=subset(df,df$index1 %in% list.1[i] & df$index2 %in% i2)#subseting data 
     x=subset(x,select=c("var1","var2"))#second subset is not needed for example but it exists in my loop 
     MyCalcs=data.frame(
      "INDEX1"=list.1[i], 
      "CALC1"=mean(x$var1+x$var2), 
      "CALC2"=mean(x$var1-x$var2), 
      "CALC3"=mean(x$var1*x$var2) 
     )#here I make some simple calculation 
     print(MyCalcs)#this i want put into data.frame 
    } 
} 

對於使用do.call(rbind,list)作品以及單迴路,但在這種情況下,結果是最後2行的print(MyCalcs)。我也試過assign但沒有成功。

+0

可以顯示基於對數據樣本所需的輸出? –

回答

1

我會通過初始化數據集並添加行來解決這個問題。這避免了使用rbind。我的方法在編制索引時容易出錯,所以我將第二個循環的索引變量更改爲與第一個循環中的索引變量不同的變量。

#first i create sample df with 3 random variables and index 
df=data.frame("var1"=runif(18,min=0,max=1), 
      "var2"=runif(18,min=0,max=1), 
      "var3"=runif(18,min=0,max=1), 
      "index2"=c(rep(c("A","B","C"),6)), 
      "index1"=c(rep(1,9),rep(2,9))) 

#lists for subseting data in loops 
list.1=list(1,2) 
list.2=list("A","B","C") 
#here I initialize the dataset 
MyCalcs.tot <- as.data.frame(matrix(rep(NA, length(list.1)*length(list.2)*4), ncol = 4)) 
names(MyCalcs.tot) <- c("INDEX1","CALC1", "CALC2", "CALC3") 

#first loop based on list.2 
for (i in 1:length(list.2)){ 
    i2=list.2[i]#indicator for inside loop to subset based on letter 
    #your second loop used the same index as the first, 
    #this migth lead to confusion, thus i changed it to a j 
    for (j in 1:length(list.1)){ 
    x=subset(df,df$index1 %in% list.1[j] & df$index2 %in% i2)#subseting data 
    x=subset(x,select=c("var1","var2"))#second subset is not needed for example but it exists in my loop 

    MyCalcs=data.frame(
     "INDEX1"=list.1[j], 
     "CALC1"=mean(x$var1+x$var2), 
     "CALC2"=mean(x$var1-x$var2), 
     "CALC3"=mean(x$var1*x$var2) 
    )#here I make some simple calculation 
    MyCalcs.tot[(i - 1)*length(list.1) + j,] <- MyCalcs #adding your calculations to the next row. 
    print(MyCalcs)#this i want put into data.frame 
    }} 

MyCalcs.tot是所需的數據幀

你也可以完全避免使用循環和使用應用功能:

#first i create sample df with 3 random variables and index 
df=data.frame("var1"=runif(48,min=0,max=1), 
       "var2"=runif(48,min=0,max=1), 
       "var3"=runif(48,min=0,max=1), 
       "index3"=c(rep(c("do","re","mi","fa"),12)), 
       "index2"=c(rep(c("A","B","C"),16)), 
       "index1"=c(rep(1,24),rep(2,24))) 

comb <- as.data.frame(cbind(unlist(lapply(list.1,function(x)rep(x,length(list.2)*length(list.3)))), 
       rep(unlist(lapply(list.2,function(x)rep(x,length(list.3)))),length(list.1)), 
       rep(unlist(list.3),length(list.1)*length(list.2)))) 
names(comb) <- c("INDEX1","INDEX2","INDEX3") 
comb$CALC1 <- apply(comb,1,function(x)mean(apply(df[,1:2],1,function(y)y[1]+ y[2])[which(df$index1 == x[1] & df$index2 == x[2] & df$index3 == x[3])])) 
comb$CALC2 <- apply(comb,1,function(x)mean(apply(df[,1:2],1,function(y)y[1]- y[2])[which(df$index1 == x[1] & df$index2 == x[2] & df$index3 == x[3])])) 
comb$CALC3 <- apply(comb,1,function(x)mean(apply(df[,1:2],1,function(y)y[1]* y[2])[which(df$index1 == x[1] & df$index2 == x[2] & df$index3 == x[3])])) 
+0

它工作膨脹,但是當我嘗試將它適配到我的更復雜版本的代碼時,我對此代碼有一些麻煩。幸運的是,它很容易重現我的例子 - 所以可以說我想添加新列到'MyCalcs',讓它成爲'list.2'和'list.2 = c(「A」,「B」,「C」 )'as so adjusted: 'MyCalcs.tot < - as.data.frame(matrix(rep(NA,length(list.1)* length(list.2)* 5),ncol = 5))'''名稱(MyCalcs.tot)< - c(「INDEX1」,「INDEX2」,「CALC1」,「CALC2」,「CALC3」)'(將其替換爲您的代碼) – Alexandros

+0

並在MyCalcs數據框中添加另一列: '「INDEX2」= list.2 [j]'(在[INDEX1 = list.1 [i]'下面加上它)。 「MyCalcs.tot」中的「INDEX2」列應該包含值「A」,「B」,「C」,但不是全部是「MyCalcs.tot」。 – Alexandros

+0

對不起分割 – Alexandros