简体   繁体   English

用R创建3D矩阵?

[英]Creating a 3D matrix with R?

I'm trying to build a 3D matrix by looping variables in a large data set (please see the 'head' of the data below). 我正在尝试通过循环大型数据集中的变量来构建3D矩阵(请参见下面数据的“头部”)。 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 (ie, 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. 具体来说,我需要创建一个矩阵,其中行数与“ LastFixes”的最大数量相同,列数与主题数(Sub = 36)和页面数与条件数(即8)相同,我想要从FixStart开始到FixEnd结束连续分配1,从FixEnd结束到FixStart开始连续分配0,依此类推。 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! 因此,例如,对于数据的第一行,我需要在矩阵的前165行中添加1,然后从第166行到330之前为0。我的R代码有效,但是奇怪的是,我得到的数字不是0和矩阵中为1s! Below you can see the 'head' and as well as the R code. 在下面,您可以看到“ head”和R代码。 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
    }
  }
}

I believe this is what you are looking for. 我相信这就是您要寻找的。 I have written a double loop, for addressing each Subject X Condtion: 我编写了一个双循环,用于解决每个Subject X条件:

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). 然后,您的小型数据集将更改为该矩阵的第7层(条件== 7)和第1列(Sub == 1)。 Here is an image: 这是一张图片:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM