开发者

printing objects from a double for loop in R

I have code for nested for loops here. The output I would like to receive is a matrix of the means of the columns of the matrix produced by the nested loop. So, the interior loop should run 1000 simulations of a randomized vector, and run a function each time. This works fine on its own, and spits the output into R. But I want to save the output from the nested loop to an object (a matrix of 1000 rows and 11 columns), and then print only the colMeans of that matrix, to be performed by the outer loop.

I think the problem lies in the step where I开发者_如何学C assign the results of the inner loop to the obj matrix. I have tried every variation on obj[i,],obj[i],obj[[i]], etc. with no success. R tells me that it is an object of only one dimension.

x=ACexp

obj=matrix(nrow=1000,ncol=11,byrow=T)          #create an empty matrix to dump results into
for(i in 1:ncol(x)){                           #nested for loops    
  a=rep(1,times=i)                             #repeat 1 for 1:# columns in x    
  b=rep(0,times=(ncol(x)-length(a)))           #have the rest of the vector be 0    
  Inv=append(a,b)                              #append these two for the Inv vector    
  for (i in 1:1000){                         #run this vector through the simulations    
      Inv2=sample(Inv,replace=FALSE)           #randomize interactions    
      temp2=rbind(x,Inv2)    
      obj[i]<-property(temp2)                   #print results to obj matrix     
  }    
print.table(colMeans(obj))                   #get colMeans and print to excel file    
}

Any ideas how this can be fixed?


You're repeatedly printing the whole matrix to the screen as it gets modified but your comment says "print to excel file". I'm guessing you actually want to save your data out to a file. Remove print.table command all together and after your loops are completed use write.table()

write.table(colMeans(obj), 'myNewMatrixFile.csv', quote = FALSE, sep = ',', row.names = FALSE)

(my preferred options... see ?write.table to select the ones you like)


Since your code isn't reproducible, we can't quite tell what you want. However, I guess that property is returning a single number that you want to place in the right row/column place of the obj matrix, which you would refer to as obj[row,col]. But you'll have trouble with that as is, because both your loops are using the same index i. Maybe something like this will work for you.

obj <- matrix(nrow=1000,ncol=11,byrow=T)       #create an empty matrix to dump results into
for(i in 1:ncol(x)){                           #nested for loops    
  Inv <- rep(c(1,0), times=c(i, ncol(x)-i))    #repeat 1 for 1:# columns in x, then 0's   
  for (j in 1:nrow(obj)){                      #run this vector through the simulations    
      Inv2 <- sample(Inv,replace=FALSE)        #randomize interactions    
      temp2 <- rbind(x,Inv2)    
      obj[j,i] <- property(temp2)              #save results in obj matrix     
  }    
}
write.csv(colMeans(obj), 'myFile.csv')         #get colMeans and print to csv file   
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜