Assigning the results of a for loop to an empty matrix

I have one more question for brilliant minds (this site is so fascinating).

I am running some simulations on a matrix, and for this purpose I have nested loops. The first creates a vector, which increases by one at each cycle of the cycle. A nested loop starts the simulation by randomizing the vector, linking it to the matrix and calculating some simple properties on the new matrix. (For example, I used properties that will not change in the simulations, but in practice I need the simulations to get an idea of ​​the effects of the randomized vector.) The nested loop starts 100 simulations, and ultimately I want only the columns of these simulations.

Here is a sample code:

property<-function(mat){                       #where mat is a matrix
  a=sum(mat) 
  b=sum(colMeans(mat))
  c=mean(mat)
  d=sum(rowMeans(mat))
  e=nrow(mat)*ncol(mat)
  answer=list(a,b,c,d,e)
  return(answer)
  }

x=matrix(c(1,0,1,0, 0,1,1,0, 0,0,0,1, 1,0,0,0, 1,0,0,1), byrow=T, nrow=5, ncol=4)

obj=matrix(nrow=100,ncol=5,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
  I.vec=append(a,b)                            #append these two for the I vector
    for (j in 1:100){
      I.vec2=sample(I.vec,replace=FALSE)       #randomize I vector
      temp=rbind(x,I.vec2)
      prop<-property(temp)
      obj[[j]]<-prop
      }
  write.table(colMeans(obj), 'myfile.csv', quote = FALSE, sep = ',', row.names = FALSE)
  }

, , , . obj NA, , . , prop obj,

obj[j,]<-prop

R , .

!

: , re :

property<-function(mat){                       #where mat is a matrix
  a=sum(mat)
  b=sum(colMeans(mat))
  f=mean(mat)
  d=sum(rowMeans(mat))
  e=nrow(mat)*ncol(mat)
  answer=c(a,b,f,d,e)
  return(answer)
  }

x=matrix(c(1,0,1,0, 0,1,1,0, 0,0,0,1, 1,0,0,0, 1,0,0,1), byrow=T, nrow=5, ncol=4)

obj<-data.frame(a=0,b=0,f=0,d=0,e=0)            #create an empty dataframe to dump results into
obj2<-data.frame(a=0,b=0,f=0,d=0,e=0)

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
  I.vec=append(a,b)                            #append these two for the I vector
    for (j in 1:100){
      I.vec2=sample(I.vec,replace=FALSE)       #randomize I vector
      temp=rbind(x,I.vec2)
      obj[j,]<-property(temp)
      }
  obj2[i,]<-colMeans(obj)
  write.table(obj2, 'myfile.csv', quote = FALSE,
  sep = ',', row.names = FALSE, col.names=F, append=T)
  }

, myfile ( x), 10 , . ?

+5
2

property . , :

property <- function(mat)
{
  ....
  c(a, b, c, d, e)  # probably a good idea to rename your "c" variable
}

, obj , data.frame ( , ).

obj <- data.frame(a=0, b=0, c=0, ...)
for(i in 1:ncol(x))
  ....
  obj[j, ] <- property(temp)

, , write.table myfile.csv, i.

+3

rbind:

 obj <- rbind(obj, prop)
+2

All Articles