用 R 创建 3D 矩阵? [英] Creating a 3D matrix with R?
问题描述
我正在尝试通过在大型数据集中循环变量来构建 3D 矩阵(请参阅下面数据的头部").具体来说,我需要创建一个矩阵,其行数与LastFixes"的最大数量一样多,列数与主题数相同(Sub = 36),页面数与条件数相同(即 8),我想要从 FixStart 开始到 FixEnd 结束连续分配 1,从 FixEnd 结束到 FixStart 开始连续分配 0,依此类推.例如,对于数据的第一行,我需要在矩阵的前 165 行中有 1s,然后从第 166 行到 330 有 0s.我的 R 代码有效,但奇怪的是,我得到了 0 以外的数字和矩阵中的 1s!您可以在下方看到头部"以及 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). 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
}
}
}
推荐答案
我相信这就是您要寻找的.我写了一个双循环,用于解决每个主题 X 条件:
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])
您的小数据集会导致第 7 层(条件 == 7)和该矩阵的第一列(Sub == 1)发生变化.这是一张图片:
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:
这篇关于用 R 创建 3D 矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!