Creating a 3D matrix with R? -


i'm trying build 3d matrix looping variables in large data set (please see 'head' of data below). specifically, need create matrix many rows maximum number of 'lastfixes', many columns there subjects (sub = 36) , many pages there conditions (i.e., 8), , want assign 1s in row start of fixstart until end of fixend, , 0s end of fixend until start of fixstart , on , forth. example, first rows of data, need have 1s in first 165 rows of matrix , 0s row# 166 until 330. r code works, strangely, numbers other 0s , 1s in matrix! below can see 'head' , r code. grateful if me fix problem. many thanks

enter code here  head (data)    sub item condition fixstart fixend 1   1    4         7        1    165 2   1    4         7      331    600 3   1    4         7      623   1180 4   1    4         7     1202   1487 5   1    4         7     1511   1561 6   1    4         7     1696   2466   lastfix <- max(data$fixend)   datamatrix<-array(0,dim=c(lastfix,36,8))  (i in 1:length(data$sub)){    n <- data[i,1]   if (data[i,3] == "1"){     (j in data[i,4]:data[i,5]){       datamatrix[j,n,1] <- datamatrix[j,n,1]+1     }   }else    if (data[i,3] == "2"){     (j in data[i,4]:data[i,5]){       datamatrix[j,n,2] <- datamatrix[j,n,2]+1     }   }else    if (data[i,3] == "3"){     (j in data[i,4]:data[i,5]){       datamatrix[j,n,3] <- datamatrix[j,n,3]+1     }   }else   if (data[i,3] == "4"){     (j in data[i,4]:data[i,5]){       datamatrix[j,n,4] <- datamatrix[j,n,4]+1     }   }else   if (data[i,3] == "5"){     (j in data[i,4]:data[i,5]){       datamatrix[j,n,5] <- datamatrix[j,n,5]+1     }   }else   if (data[i,3] == "6"){     (j in data[i,4]:data[i,5]){       datamatrix[j,n,6] <- datamatrix[j,n,6]+1     }   }else   if (data[i,3] == "7"){     (j in data[i,4]:data[i,5]){       datamatrix[j,n,7] <- datamatrix[j,n,7]+1     }   }else   if (data[i,3] == "8"){     for(j in data[i,4]:data[i,5]){       datamatrix[j,n,8] <- datamatrix[j,n,8]+1     }   } } 

i believe looking for. have written double loop, addressing each subject x condtion:

lastfix <- max(data$fixend)  datamatrix <- array(0,dim=c(lastfix,36,8))  for(j in seq(dim(datamatrix)[2])){ #loop sub     for(k in seq(dim(datamatrix)[3])){ #loop condition         #j=1; k=7         data.sub <- subset(data, sub==j & condition==k)         if(nrow(data.sub) != 0){             for(i in seq(nrow(data.sub))){                 ones <- data.sub$fixstart[i]:data.sub$fixend[i]                 datamatrix[ones,j,k] <- 1             }         }         print(paste("sub", j, ";", "condition", k, "is finished"))     } }  #image shows 1's have been added subject 1 (column)  image(x=seq(dim(datamatrix)[1]), y=seq(dim(datamatrix)[2]), datamatrix[,,7]) 

your small data set results in changes 7th layer(condition == 7) , 1st column of matrix (sub == 1). here image:

enter image description here


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -