# [R] Random/systematic selection of rows in a matrix

Ruben Roa RRoa at fisheries.gov.fk
Thu May 19 16:17:17 CEST 2005

```Hi R people:
I am new to R. I am writing a function to (1) produce a sparse
stochastic Gaussian 2D field and (2) perform a systematic
transect sampling on this field, this carried out many times
in a simulation framework. My function does a good job at
producing the random field (a matrix of zeros and some
manifestations of the stochastic process, depending on a
parameter of the function determining how probable it is
that the stochastic process will manifest itself). But, when
i try to collect some of the rows of the process as in
sampling with transects, i noticed in the output files that
the rows in the sample were not any of the rows in the original
matrix representing the random field. I show below the function.
I believe i am making a programming error due to my inexperience
with R. I hope some fo you can show me where the error is.
Thanks
Ruben

mcolasim7<-function(N.sim,pcell){
# Lattice definition
x<-seq(1,180,1)
y<-seq(1,540,1)
#Gaussian process
param<-c(6.63,2.24,1.82,4.36)
names(param)<-c("beta","sigmasq","tausq","varphi")
#Simulations loop - Function GaussRF from package RandomFields
for(i in 1:N.sim){
mcola<-GaussRF(x=x,y=y,param=param,grid=TRUE,model="gauss")
#Process thininng out with 'pcell': probability that the process will manifest itself
for(j in 1:540){
for(k in 1:180){
if(runif(1)<pcell) mcola[k,j]<-mcola[k,j] else mcola[k,j]<-0
}
}
file1.out<-paste("mcolasim7",i,"txt",sep=".")
write(t(mcola),file1.out,ncol=ncol(t(mcola)))
#Transect sampling on the thinned process - 20 equidistant transects parallel to x axis
#First transect randomly chosen among first 27 rows in process
t=0
t<-(1+as.integer(27*runif(1)))
transis<-c(t,t+27,t+54,t+81,t+108,t+135,t+162,t+189,t+216,t+243,t+270,t+297,t+324,t+351,t+378,t+405,t+432,t+459,t+486,t+513)
mcolasamsis<-mat.or.vec(20,180)
for(l in 1:20){
mcolasamsis[l,]<-mcola[,transis[l]]
}
file2.out<-paste("mcolasamsis7",i,"txt",sep=".")
write(mcolasamsis,file2.out,ncol=ncol(mcolasamsis))
}
}

```