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){
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)
for(i in 1:ncol(x)){
a=rep(1,times=i)
b=rep(0,times=(ncol(x)-length(a)))
I.vec=append(a,b)
for (j in 1:100){
I.vec2=sample(I.vec,replace=FALSE)
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){
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)
obj2<-data.frame(a=0,b=0,f=0,d=0,e=0)
for(i in 1:ncol(x)){
a=rep(1,times=i)
b=rep(0,times=(ncol(x)-length(a)))
I.vec=append(a,b)
for (j in 1:100){
I.vec2=sample(I.vec,replace=FALSE)
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 , . ?