创建一个3D矩阵为R? [英] Creating a 3D matrix with R?
问题描述
我试图建立一个三维矩阵在一个大的数据集循环变量(请参见下面的数据的头)。具体而言,我需要创建的行数的LastFixes'的最大数量的矩阵,多列,因为有科目(次= 36),并尽可能多的网页,因为有状况(即8),我想以在从FixStart的开始的行指定1秒,直到一个FixEnd的端部,并从一个FixEnd的端0直到FixStart的启动等,等等。因此,例如,对于数据的第一行,我必须第一个165矩阵的行1和0,然后从行号166至330。我R code ++工程,但奇怪的是,我得到其他号码比0和1的矩阵!下面你可以看到头和还有R $ C $℃。我将不胜感激,如果有人可以帮助我解决这个问题。非常感谢
在此输入code
头(数据)
子项条件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< - 最大(数据$ FixEnd)
数据矩阵< -array(0,昏暗= C(lastFix,36.8))
对于(i的1:长度(数据$子)){
N'LT; - 数据[1,1]
如果(数据[I,3] ==1){
为(十在数据[I,4]:数据〔Ⅰ,5]){
数据矩阵[J,N,1]; - 数据矩阵[J,N,1] +1
}
}其他
如果(数据[I,3] ==2){
为(十在数据[I,4]:数据〔Ⅰ,5]){
数据矩阵[J,N,2]< - 数据矩阵[J,N,2] +1
}
}其他
如果(数据[I,3] ==3){
为(十在数据[I,4]:数据〔Ⅰ,5]){
数据矩阵[J,N,3]< - 数据矩阵[J,N,3] +1
}
}其他
如果(数据[I,3] ==4){
为(十在数据[I,4]:数据〔Ⅰ,5]){
数据矩阵[J,N,4]< - 数据矩阵[J,N,4] +1
}
}其他
如果(数据[I,3] ==5){
为(十在数据[I,4]:数据〔Ⅰ,5]){
数据矩阵[J,N,5]< - 数据矩阵[J,N,5] +1
}
}其他
如果(数据[I,3] ==6){
为(十在数据[I,4]:数据〔Ⅰ,5]){
数据矩阵[J,N,6]< - 数据矩阵[J,N,6] +1
}
}其他
如果(数据[I,3] ==7){
为(十在数据[I,4]:数据〔Ⅰ,5]){
数据矩阵[J,N,7]< - 数据矩阵[J,N,7] +1
}
}其他
如果(数据[I,3] ==8){
为(十在数据[I,4]:数据〔Ⅰ,5]){
数据矩阵[J,N,8]< - 数据矩阵[J,N,8] +1
}
}
}
我相信这是你在找什么。我写一个双循环,解决每个主体X Condtion:
lastFix< - 最大(数据$ FixEnd)
数据矩阵< - 数组(0,昏暗= C(lastFix,36.8))
对于(十序列(变暗(数据矩阵)[2])){#LOOP的子
对于(K序列(变暗(数据矩阵)[3])){#LOOP的条件
#J = 1;程k = 7
data.sub&所述; - 子集(数据,分== J和条件== k)的
如果(nrow(data.sub)!= 0){
对于(i的序列(nrow(data.sub))){
那些< - data.sub $ FixStart [我]:data.sub $ FixEnd [I]
数据矩阵[那些,J,K]其中; - 1
}
}
印(贴(子,J,;,条件,K,结束))
}
}
#image表明1的已被添加到被检体1(列)
图像(X = SEQ(点心(数据矩阵)[1]),Y = SEQ(点心(数据矩阵)[2]),数据矩阵[,, 7])
设置你的小数据,然后导致的变化到第7层基质(子== 1)的(条件== 7)和第1列。以下是图像:
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
}
}
}
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:
这篇关于创建一个3D矩阵为R?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!