Bcampbell99
2012-Apr-05 17:13 UTC
[R] Multi part problem...array manipulation and sampling
Ok, I have a new, multipart problem that I need help figuring out. Part 1. I have a three dimensional array (species, sites, repeat counts within sites). Sampling effort per site varies so the array should be ragged. Maximum number of visits at any site = 22 Number of species = 161 Number of sites = 56 I generated the array first by; mydata<-tapply(spdata1$NTOTAL,list(spdata1$COUNT,spdata1$SPECIES,spdata1$SITESURVEY),sum) where spdata1$NTOTAL = number of detections spdata1$COUNT = repeat visit per site (max = 22) spdata1$SITESURVEY = site and survey period (=56) This gives me an array with dim (22,161,56), which is populated by either a number (NTOTAL), or an NA. I then converted the NAs to zeroes; mydata[is.na(mydata)] <- 0 To get the total number of records per sitesurvey i used; array.dat1<-array(0,dim=c(22,1,56)) for (i in 1:56){ array.dat1[,,i]<-rowSums(mydata1[,,i],1) } array.dat1[which(array.dat1==0)] = NA SO when array.dat1=0, that represents that there was no survey. To combine this information with the original matrix i used new.mydata<-array(0,dim=c(22,162,56)) new.mydata[1:22,1:161,1:56]<-mydata new.mydata[1:22,162,1:56]<-array.dat1 where [,162,] is the index that represents whether a survey had been conducted or not...sitesurvey total OK, here is where I need help; Step 1) I need to extract from new.mydata1 the rows where [,162,] !NA....this will create a ragged array where the number of counts per sitesurvey varies outdata (dim1 varies from 1,22...ragged, 162,56). Step 2) from the extracted list, I would like to randomly select n rows (say for example, 3) outdata (dim1=2 or 3 counts(samples), dim2=162, dim3=56) Step 3) I need to sum the values from the columns of dimension 1 from step 2 s outdata (dim1=1 (column sums), dim2=162, dim3=56) Not sure how clear I've explained this, but any suggestions would be really helpful. Most appreciatively: Brian Campbell -- View this message in context: http://r.789695.n4.nabble.com/Multi-part-problem-array-manipulation-and-sampling-tp4535476p4535476.html Sent from the R help mailing list archive at Nabble.com.