Creating a 3D matrix with R?

2019-07-18 04:58发布

I'm trying to build a 3D matrix by looping variables in a large data set (please see the 'head' of the data below). Specifically, I need to create a matrix with as many rows as the maximum number of 'LastFixes', as many columns as there are subjects (Sub = 36) and as many pages as there are conditions (i.e., 8), and I want to assign 1s in a row from the start of FixStart up until the end of a FixEnd, and 0s from end of a FixEnd until the start of FixStart and so on and so forth. So for example, for the first rows of the data, I need to have 1s in the first 165 rows of the matrix and then 0s from row# 166 until 330. My R code works, but strangely, I get numbers other than 0s and 1s in the matrix! Below you can see the 'head' and as well as the R code. I would be grateful if anyone could help me fix this 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))

for (i in 1:length(data$Sub)){ 
  n <- data[i,1]
  if (data[i,3] == "1"){
    for (j in data[i,4]:data[i,5]){
      datamatrix[j,n,1] <- datamatrix[j,n,1]+1
    }
  }else 
  if (data[i,3] == "2"){
    for (j in data[i,4]:data[i,5]){
      datamatrix[j,n,2] <- datamatrix[j,n,2]+1
    }
  }else 
  if (data[i,3] == "3"){
    for (j in data[i,4]:data[i,5]){
      datamatrix[j,n,3] <- datamatrix[j,n,3]+1
    }
  }else
  if (data[i,3] == "4"){
    for (j in data[i,4]:data[i,5]){
      datamatrix[j,n,4] <- datamatrix[j,n,4]+1
    }
  }else
  if (data[i,3] == "5"){
    for (j in data[i,4]:data[i,5]){
      datamatrix[j,n,5] <- datamatrix[j,n,5]+1
    }
  }else
  if (data[i,3] == "6"){
    for (j in data[i,4]:data[i,5]){
      datamatrix[j,n,6] <- datamatrix[j,n,6]+1
    }
  }else
  if (data[i,3] == "7"){
    for (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
    }
  }
}

标签: r matrix 3d
1条回答
【Aperson】
2楼-- · 2019-07-18 05:28

I believe this is what you are looking for. I have written a double loop, for 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 for Sub
    for(k in seq(dim(datamatrix)[3])){ #loop for 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 that 1's have been added to Subject 1 (column) 
image(x=seq(dim(datamatrix)[1]), y=seq(dim(datamatrix)[2]), datamatrix[,,7])

Your small data set then results in changes to the 7th layer(Condition == 7) and 1st column of that matrix (Sub == 1). Here is an image:

enter image description here

查看更多
登录 后发表回答